135c990c652af46f4ab81d13997255ff1bd7bf38
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / UserInput.cpp
1 //===-- UserInput.cpp - Interpreter Input Loop support --------------------===//
2 // 
3 //  This file implements the interpreter Input I/O loop.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "Interpreter.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/Function.h"
10 #include "llvm/Module.h"
11
12 // callMainFunction - This is a nasty gross hack that will dissapear when
13 // callFunction can parse command line options and stuff for us.
14 //
15 bool Interpreter::callMainFunction(const std::string &Name,
16                                    const std::vector<std::string> &InputArgv) {
17   Function *M = getModule().getNamedFunction(Name);
18   if (M == 0) {
19     std::cerr << "Could not find function '" << Name << "' in module!\n";
20     return 1;
21   }
22   const FunctionType *MT = M->getFunctionType();
23
24   std::vector<GenericValue> Args;
25   if (MT->getParamTypes().size() >= 2) {
26   PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
27   if (MT->getParamTypes()[1] != SPP) {
28     CW << "Second argument of '" << Name << "' should have type: '"
29        << SPP << "'!\n";
30     return true;
31   }
32   Args.push_back(PTOGV(CreateArgv(InputArgv)));
33   }
34
35   if (MT->getParamTypes().size() >= 1) {
36   if (!MT->getParamTypes()[0]->isInteger()) {
37     std::cout << "First argument of '" << Name << "' should be an integer!\n";
38     return true;
39   } else {
40     GenericValue GV; GV.UIntVal = InputArgv.size();
41     Args.insert(Args.begin(), GV);
42   }
43   }
44
45   callFunction(M, Args);  // Start executing it...
46
47   // Reset the current frame location to the top of stack
48   CurFrame = ECStack.size()-1;
49
50   return false;
51 }