llvm.org GIT mirror llvm / c48e1db
remove EH cruft, add bitcode support git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36841 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
4 changed file(s) with 153 addition(s) and 125 deletion(s). Raw diff Collapse all Expand all
55 # the University of Illinois Open Source License. See LICENSE.TXT for details.
66 #
77 ##===----------------------------------------------------------------------===##
8
89 LEVEL = ../..
910
1011 TOOLNAME = llvm-extract
11 LINK_COMPONENTS := bcreader bcwriter ipo
12 REQUIRES_EH := 1
12 LINK_COMPONENTS := bcreader bcwriter ipo bitreader bitwriter
1313
1414 include $(LEVEL)/Makefile.common
1313
1414 #include "llvm/Module.h"
1515 #include "llvm/PassManager.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
1617 #include "llvm/Bytecode/Reader.h"
1718 #include "llvm/Bytecode/WriteBytecodePass.h"
1819 #include "llvm/Transforms/IPO.h"
2021 #include "llvm/Support/CommandLine.h"
2122 #include "llvm/Support/Compressor.h"
2223 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
2325 #include "llvm/Support/Streams.h"
2426 #include "llvm/System/Signals.h"
2527 #include
2628 #include
2729 #include
2830 using namespace llvm;
31
32 cl::opt Bitcode("bitcode");
2933
3034 // InputFilename - The filename to read from.
3135 static cl::opt
5357
5458 int main(int argc, char **argv) {
5559 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
56 try {
57 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
58 sys::PrintStackTraceOnErrorSignal();
60 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
61 sys::PrintStackTraceOnErrorSignal();
5962
60 std::auto_ptr M(ParseBytecodeFile(InputFilename,
61 Compressor::decompressToNewBuffer));
62 if (M.get() == 0) {
63 cerr << argv[0] << ": bytecode didn't read correctly.\n";
63 std::auto_ptr M;
64
65 if (Bitcode) {
66 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&InputFilename[0],
67 InputFilename.size());
68 if (Buffer == 0) {
69 cerr << "Error reading file '" + InputFilename + "'";
70 return 1;
71 } else {
72 M.reset(ParseBitcodeFile(Buffer));
73 }
74 delete Buffer;
75 } else {
76 M.reset(ParseBytecodeFile(InputFilename,
77 Compressor::decompressToNewBuffer));
78 }
79
80 if (M.get() == 0) {
81 cerr << argv[0] << ": bytecode didn't read correctly.\n";
82 return 1;
83 }
84
85 // Figure out which function we should extract
86 Function *F = M.get()->getFunction(ExtractFunc);
87 if (F == 0) {
88 cerr << argv[0] << ": program doesn't contain function named '"
89 << ExtractFunc << "'!\n";
90 return 1;
91 }
92
93 // In addition to deleting all other functions, we also want to spiff it
94 // up a little bit. Do this now.
95 PassManager Passes;
96 Passes.add(new TargetData(M.get())); // Use correct TargetData
97 // Either isolate the function or delete it from the Module
98 Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
99 if (!DeleteFn)
100 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
101 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
102 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
103
104 std::ostream *Out = 0;
105
106 if (OutputFilename != "-") { // Not stdout?
107 if (!Force && std::ifstream(OutputFilename.c_str())) {
108 // If force is not specified, make sure not to overwrite a file!
109 cerr << argv[0] << ": error opening '" << OutputFilename
110 << "': file exists!\n"
111 << "Use -f command line argument to force output\n";
64112 return 1;
65113 }
114 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
115 std::ios::binary;
116 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
117 } else { // Specified stdout
118 // FIXME: cout is not binary!
119 Out = &std::cout;
120 }
66121
67 // Figure out which function we should extract
68 Function *F = M.get()->getFunction(ExtractFunc);
69 if (F == 0) {
70 cerr << argv[0] << ": program doesn't contain function named '"
71 << ExtractFunc << "'!\n";
72 return 1;
73 }
122 OStream L(*Out);
123 if (Bitcode)
124 Passes.add(CreateBitcodeWriterPass(*Out));
125 else
126 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
127 Passes.run(*M.get());
74128
75 // In addition to deleting all other functions, we also want to spiff it
76 // up a little bit. Do this now.
77 PassManager Passes;
78 Passes.add(new TargetData(M.get())); // Use correct TargetData
79 // Either isolate the function or delete it from the Module
80 Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
81 if (!DeleteFn)
82 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
83 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
84 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
85
86 std::ostream *Out = 0;
87
88 if (OutputFilename != "-") { // Not stdout?
89 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
91 cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
94 return 1;
95 }
96 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
97 std::ios::binary;
98 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
99 } else { // Specified stdout
100 // FIXME: cout is not binary!
101 Out = &std::cout;
102 }
103
104 OStream L(*Out);
105 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
106 Passes.run(*M.get());
107
108 if (Out != &std::cout)
109 delete Out;
110 return 0;
111 } catch (const std::string& msg) {
112 cerr << argv[0] << ": " << msg << "\n";
113 } catch (...) {
114 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
115 }
116 return 1;
129 if (Out != &std::cout)
130 delete Out;
131 return 0;
117132 }
88 LEVEL = ../..
99
1010 TOOLNAME = llvm-link
11 LINK_COMPONENTS = linker bcreader bcwriter
12 REQUIRES_EH := 1
11 LINK_COMPONENTS = linker bcreader bcwriter bitreader bitwriter
1312
1413 include $(LEVEL)/Makefile.common
1414 #include "llvm/Linker.h"
1515 #include "llvm/Module.h"
1616 #include "llvm/Analysis/Verifier.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
1718 #include "llvm/Bytecode/Reader.h"
1819 #include "llvm/Bytecode/Writer.h"
1920 #include "llvm/Support/CommandLine.h"
2021 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/MemoryBuffer.h"
2123 #include "llvm/Support/Streams.h"
2224 #include "llvm/System/Signals.h"
2325 #include "llvm/System/Path.h"
2527 #include
2628 #include
2729 using namespace llvm;
30
31 cl::opt Bitcode("bitcode");
2832
2933 static cl::list
3034 InputFilenames(cl::Positional, cl::OneOrMore,
5862 std::string ErrorMessage;
5963 if (Filename.exists()) {
6064 if (Verbose) cerr << "Loading '" << Filename.c_str() << "'\n";
61 Module* Result = ParseBytecodeFile(Filename.toString(),
62 Compressor::decompressToNewBuffer,
63 &ErrorMessage);
65 Module* Result = 0;
66
67 if (Bitcode) {
68 const std::string &FNStr = Filename.toString();
69 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&FNStr[0],
70 FNStr.size());
71 if (Buffer == 0)
72 ErrorMessage = "Error reading file '" + FNStr + "'";
73 else
74 Result = ParseBitcodeFile(Buffer, &ErrorMessage);
75 delete Buffer;
76 } else {
77 Result = ParseBytecodeFile(Filename.toString(),
78 Compressor::decompressToNewBuffer,
79 &ErrorMessage);
80 }
6481 if (Result) return std::auto_ptr(Result); // Load successful!
6582
6683 if (Verbose) {
7794
7895 int main(int argc, char **argv) {
7996 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
80 try {
81 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
82 sys::PrintStackTraceOnErrorSignal();
83 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
97 cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
98 sys::PrintStackTraceOnErrorSignal();
99 assert(InputFilenames.size() > 0 && "OneOrMore is not working");
84100
85 unsigned BaseArg = 0;
86 std::string ErrorMessage;
101 unsigned BaseArg = 0;
102 std::string ErrorMessage;
87103
88 std::auto_ptr Composite(LoadFile(InputFilenames[BaseArg]));
89 if (Composite.get() == 0) {
90 cerr << argv[0] << ": error loading file '"
91 << InputFilenames[BaseArg] << "'\n";
104 std::auto_ptr Composite(LoadFile(InputFilenames[BaseArg]));
105 if (Composite.get() == 0) {
106 cerr << argv[0] << ": error loading file '"
107 << InputFilenames[BaseArg] << "'\n";
108 return 1;
109 }
110
111 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
112 std::auto_ptr M(LoadFile(InputFilenames[i]));
113 if (M.get() == 0) {
114 cerr << argv[0] << ": error loading file '" <
92115 return 1;
93116 }
94117
95 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
96 std::auto_ptr M(LoadFile(InputFilenames[i]));
97 if (M.get() == 0) {
98 cerr << argv[0] << ": error loading file '" <
99 return 1;
100 }
118 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
101119
102 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
120 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
121 cerr << argv[0] << ": link error in '" << InputFilenames[i]
122 << "': " << ErrorMessage << "\n";
123 return 1;
124 }
125 }
103126
104 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
105 cerr << argv[0] << ": link error in '" << InputFilenames[i]
106 << "': " << ErrorMessage << "\n";
107 return 1;
108 }
127 // TODO: Iterate over the -l list and link in any modules containing
128 // global symbols that have not been resolved so far.
129
130 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
131
132 // FIXME: cout is not binary!
133 std::ostream *Out = &std::cout; // Default to printing to stdout...
134 if (OutputFilename != "-") {
135 if (!Force && std::ifstream(OutputFilename.c_str())) {
136 // If force is not specified, make sure not to overwrite a file!
137 cerr << argv[0] << ": error opening '" << OutputFilename
138 << "': file exists!\n"
139 << "Use -f command line argument to force output\n";
140 return 1;
109141 }
110
111 // TODO: Iterate over the -l list and link in any modules containing
112 // global symbols that have not been resolved so far.
113
114 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
115
116 // FIXME: cout is not binary!
117 std::ostream *Out = &std::cout; // Default to printing to stdout...
118 if (OutputFilename != "-") {
119 if (!Force && std::ifstream(OutputFilename.c_str())) {
120 // If force is not specified, make sure not to overwrite a file!
121 cerr << argv[0] << ": error opening '" << OutputFilename
122 << "': file exists!\n"
123 << "Use -f command line argument to force output\n";
124 return 1;
125 }
126 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
127 std::ios::binary;
128 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
129 if (!Out->good()) {
130 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
131 return 1;
132 }
133
134 // Make sure that the Out file gets unlinked from the disk if we get a
135 // SIGINT
136 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
137 }
138
139 if (verifyModule(*Composite.get())) {
140 cerr << argv[0] << ": linked module is broken!\n";
142 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
143 std::ios::binary;
144 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
145 if (!Out->good()) {
146 cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
141147 return 1;
142148 }
143149
144 if (Verbose) cerr << "Writing bytecode...\n";
150 // Make sure that the Out file gets unlinked from the disk if we get a
151 // SIGINT
152 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
153 }
154
155 if (verifyModule(*Composite.get())) {
156 cerr << argv[0] << ": linked module is broken!\n";
157 return 1;
158 }
159
160 if (Verbose) cerr << "Writing bytecode...\n";
161 if (Bitcode) {
162 WriteBitcodeToFile(Composite.get(), *Out);
163 } else {
145164 OStream L(*Out);
146165 WriteBytecodeToFile(Composite.get(), L, !NoCompress);
166 }
147167
148 if (Out != &std::cout) delete Out;
149 return 0;
150 } catch (const std::string& msg) {
151 cerr << argv[0] << ": " << msg << "\n";
152 } catch (...) {
153 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
154 }
155 return 1;
168 if (Out != &std::cout) delete Out;
169 return 0;
156170 }