blob: 135c990c652af46f4ab81d13997255ff1bd7bf38 [file] [log] [blame]
Chris Lattnerd7ff5782001-08-23 17:05:04 +00001//===-- UserInput.cpp - Interpreter Input Loop support --------------------===//
2//
3// This file implements the interpreter Input I/O loop.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Interpreter.h"
Chris Lattnerd299dba2001-10-18 21:55:32 +00008#include "llvm/DerivedTypes.h"
Chris Lattnera0d7b082002-12-23 23:59:41 +00009#include "llvm/Function.h"
Brian Gaekee80e5ba2003-09-04 22:21:24 +000010#include "llvm/Module.h"
Chris Lattnerd299dba2001-10-18 21:55:32 +000011
Chris Lattner470754e2003-05-08 16:18:31 +000012// callMainFunction - This is a nasty gross hack that will dissapear when
13// callFunction can parse command line options and stuff for us.
Chris Lattnerd299dba2001-10-18 21:55:32 +000014//
Chris Lattner470754e2003-05-08 16:18:31 +000015bool Interpreter::callMainFunction(const std::string &Name,
16 const std::vector<std::string> &InputArgv) {
Brian Gaekee80e5ba2003-09-04 22:21:24 +000017 Function *M = getModule().getNamedFunction(Name);
18 if (M == 0) {
19 std::cerr << "Could not find function '" << Name << "' in module!\n";
20 return 1;
Chris Lattnerd299dba2001-10-18 21:55:32 +000021 }
Chris Lattnerf94811a2002-03-29 03:57:15 +000022 const FunctionType *MT = M->getFunctionType();
Chris Lattnerd299dba2001-10-18 21:55:32 +000023
Chris Lattner7f74a562002-01-20 22:54:45 +000024 std::vector<GenericValue> Args;
Brian Gaekee80e5ba2003-09-04 22:21:24 +000025 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";
Chris Lattnerd299dba2001-10-18 21:55:32 +000030 return true;
Chris Lattnerd299dba2001-10-18 21:55:32 +000031 }
Brian Gaekee80e5ba2003-09-04 22:21:24 +000032 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 }
Chris Lattnerd299dba2001-10-18 21:55:32 +000043 }
44
Chris Lattner470754e2003-05-08 16:18:31 +000045 callFunction(M, Args); // Start executing it...
Chris Lattnerd299dba2001-10-18 21:55:32 +000046
47 // Reset the current frame location to the top of stack
48 CurFrame = ECStack.size()-1;
49
50 return false;
51}