llvm.org GIT mirror llvm / c1e6d68
add bitcode reading support, remove eh stuff git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36840 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 96 addition(s) and 87 deletion(s). Raw diff Collapse all Expand all
88
99 LEVEL := ../..
1010 TOOLNAME := lli
11 LINK_COMPONENTS := jit interpreter native bcreader selectiondag
12 REQUIRES_EH := 1
11 LINK_COMPONENTS := jit interpreter native bcreader bitreader selectiondag
1312
1413 # Enable JIT support
1514 include $(LEVEL)/Makefile.common
1515 #include "llvm/Module.h"
1616 #include "llvm/ModuleProvider.h"
1717 #include "llvm/Type.h"
18 #include "llvm/Bitcode/ReaderWriter.h"
1819 #include "llvm/Bytecode/Reader.h"
1920 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
2021 #include "llvm/ExecutionEngine/JIT.h"
2324 #include "llvm/Support/CommandLine.h"
2425 #include "llvm/Support/Compressor.h"
2526 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/MemoryBuffer.h"
2628 #include "llvm/Support/PluginLoader.h"
2729 #include "llvm/System/Process.h"
2830 #include "llvm/System/Signals.h"
3133 using namespace llvm;
3234
3335 namespace {
36 cl::opt Bitcode("bitcode");
37
3438 cl::opt
3539 InputFile(cl::desc(""), cl::Positional, cl::init("-"));
3640
6569 //
6670 int main(int argc, char **argv, char * const *envp) {
6771 atexit(do_shutdown); // Call llvm_shutdown() on exit.
68 try {
69 cl::ParseCommandLineOptions(argc, argv,
70 " llvm interpreter & dynamic compiler\n");
71 sys::PrintStackTraceOnErrorSignal();
72 cl::ParseCommandLineOptions(argc, argv,
73 " llvm interpreter & dynamic compiler\n");
74 sys::PrintStackTraceOnErrorSignal();
7275
73 // If the user doesn't want core files, disable them.
74 if (DisableCoreFiles)
75 sys::Process::PreventCoreFiles();
76
77 // Load the bytecode...
78 std::string ErrorMsg;
79 ModuleProvider *MP = getBytecodeModuleProvider(InputFile,
80 Compressor::decompressToNewBuffer,
81 &ErrorMsg);
82 if (!MP) {
83 std::cerr << argv[0] << ": error loading program '" << InputFile << "': "
84 << ErrorMsg << "\n";
85 exit(1);
76 // If the user doesn't want core files, disable them.
77 if (DisableCoreFiles)
78 sys::Process::PreventCoreFiles();
79
80 // Load the bytecode...
81 std::string ErrorMsg;
82 ModuleProvider *MP = 0;
83 if (Bitcode) {
84 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&InputFile[0],
85 InputFile.size());
86 if (Buffer == 0)
87 ErrorMsg = "Error reading file '" + InputFile + "'";
88 else {
89 MP = getBitcodeModuleProvider(Buffer, &ErrorMsg);
90 if (!MP) delete Buffer;
8691 }
92 } else {
93 MP = getBytecodeModuleProvider(InputFile,
94 Compressor::decompressToNewBuffer,
95 &ErrorMsg);
96 }
97
98 if (!MP) {
99 std::cerr << argv[0] << ": error loading program '" << InputFile << "': "
100 << ErrorMsg << "\n";
101 exit(1);
102 }
87103
88 // Get the module as the MP could go away once EE takes over.
89 Module *Mod = MP->getModule();
104 // Get the module as the MP could go away once EE takes over.
105 Module *Mod = MP->getModule();
90106
91 // If we are supposed to override the target triple, do so now.
92 if (!TargetTriple.empty())
93 Mod->setTargetTriple(TargetTriple);
94
95 EE = ExecutionEngine::create(MP, ForceInterpreter, &ErrorMsg);
96 if (!EE && !ErrorMsg.empty()) {
97 std::cerr << argv[0] << ":error creating EE: " << ErrorMsg << "\n";
98 exit(1);
99 }
107 // If we are supposed to override the target triple, do so now.
108 if (!TargetTriple.empty())
109 Mod->setTargetTriple(TargetTriple);
110
111 EE = ExecutionEngine::create(MP, ForceInterpreter, &ErrorMsg);
112 if (!EE && !ErrorMsg.empty()) {
113 std::cerr << argv[0] << ":error creating EE: " << ErrorMsg << "\n";
114 exit(1);
115 }
100116
101 // If the user specifically requested an argv[0] to pass into the program,
102 // do it now.
103 if (!FakeArgv0.empty()) {
104 InputFile = FakeArgv0;
105 } else {
106 // Otherwise, if there is a .bc suffix on the executable strip it off, it
107 // might confuse the program.
108 if (InputFile.rfind(".bc") == InputFile.length() - 3)
109 InputFile.erase(InputFile.length() - 3);
110 }
117 // If the user specifically requested an argv[0] to pass into the program,
118 // do it now.
119 if (!FakeArgv0.empty()) {
120 InputFile = FakeArgv0;
121 } else {
122 // Otherwise, if there is a .bc suffix on the executable strip it off, it
123 // might confuse the program.
124 if (InputFile.rfind(".bc") == InputFile.length() - 3)
125 InputFile.erase(InputFile.length() - 3);
126 }
111127
112 // Add the module's name to the start of the vector of arguments to main().
113 InputArgv.insert(InputArgv.begin(), InputFile);
128 // Add the module's name to the start of the vector of arguments to main().
129 InputArgv.insert(InputArgv.begin(), InputFile);
114130
115 // Call the main function from M as if its signature were:
116 // int main (int argc, char **argv, const char **envp)
117 // using the contents of Args to determine argc & argv, and the contents of
118 // EnvVars to determine envp.
119 //
120 Function *Fn = Mod->getFunction("main");
121 if (!Fn) {
122 std::cerr << "'main' function not found in module.\n";
123 return -1;
124 }
131 // Call the main function from M as if its signature were:
132 // int main (int argc, char **argv, const char **envp)
133 // using the contents of Args to determine argc & argv, and the contents of
134 // EnvVars to determine envp.
135 //
136 Function *Fn = Mod->getFunction("main");
137 if (!Fn) {
138 std::cerr << "'main' function not found in module.\n";
139 return -1;
140 }
125141
126 // If the program doesn't explicitly call exit, we will need the Exit
127 // function later on to make an explicit call, so get the function now.
128 Constant *Exit = Mod->getOrInsertFunction("exit", Type::VoidTy,
129 Type::Int32Ty, NULL);
130
131 // Reset errno to zero on entry to main.
132 errno = 0;
133
134 // Run static constructors.
135 EE->runStaticConstructorsDestructors(false);
136
137 // Run main.
138 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
142 // If the program doesn't explicitly call exit, we will need the Exit
143 // function later on to make an explicit call, so get the function now.
144 Constant *Exit = Mod->getOrInsertFunction("exit", Type::VoidTy,
145 Type::Int32Ty, NULL);
146
147 // Reset errno to zero on entry to main.
148 errno = 0;
149
150 // Run static constructors.
151 EE->runStaticConstructorsDestructors(false);
152
153 // Run main.
154 int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
139155
140 // Run static destructors.
141 EE->runStaticConstructorsDestructors(true);
142
143 // If the program didn't call exit explicitly, we should call it now.
144 // This ensures that any atexit handlers get called correctly.
145 if (Function *ExitF = dyn_cast(Exit)) {
146 std::vector Args;
147 GenericValue ResultGV;
148 ResultGV.IntVal = APInt(32, Result);
149 Args.push_back(ResultGV);
150 EE->runFunction(ExitF, Args);
151 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
152 abort();
153 } else {
154 std::cerr << "ERROR: exit defined with wrong prototype!\n";
155 abort();
156 }
157 } catch (const std::string& msg) {
158 std::cerr << argv[0] << ": " << msg << "\n";
159 } catch (...) {
160 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
156 // Run static destructors.
157 EE->runStaticConstructorsDestructors(true);
158
159 // If the program didn't call exit explicitly, we should call it now.
160 // This ensures that any atexit handlers get called correctly.
161 if (Function *ExitF = dyn_cast(Exit)) {
162 std::vector Args;
163 GenericValue ResultGV;
164 ResultGV.IntVal = APInt(32, Result);
165 Args.push_back(ResultGV);
166 EE->runFunction(ExitF, Args);
167 std::cerr << "ERROR: exit(" << Result << ") returned!\n";
168 abort();
169 } else {
170 std::cerr << "ERROR: exit defined with wrong prototype!\n";
171 abort();
161172 }
162 abort();
163173 }