Enhancements to pass argc & argv to main if required
authorChris Lattner <sabre@nondot.org>
Thu, 18 Oct 2001 21:55:32 +0000 (21:55 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 18 Oct 2001 21:55:32 +0000 (21:55 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@909 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/Interpreter/Execution.cpp
lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp
lib/ExecutionEngine/Interpreter/Interpreter.h
lib/ExecutionEngine/Interpreter/UserInput.cpp
tools/lli/lli.cpp

index 315b9e8fb4a1623193b0dd7b63ad3f1705de1fda..b8cb4909342a29b3b940a63d46b0b560bc9d51f0 100644 (file)
@@ -777,6 +777,8 @@ void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
 
 
   // Run through the method arguments and initialize their values...
+  assert(ArgVals.size() == M->getArgumentList().size() &&
+         "Invalid number of values passed to method invocation!");
   unsigned i = 0;
   for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
         ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
index 7be3336c5865ca29be2ab4dc3cf77ad71203f0ab..bcd584a6b5c4a834b168fd9b122a9d67fbf7b911 100644 (file)
@@ -110,9 +110,10 @@ GenericValue lle_X_print(MethodType *M, const vector<GenericValue> &ArgVals) {
 GenericValue lle_X_printVal(MethodType *M, const vector<GenericValue> &ArgVal) {
   assert(ArgVal.size() == 1 && "generic print only takes one argument!");
 
-  // Specialize print([ubyte {x N} ] *)
+  // Specialize print([ubyte {x N} ] *) and print(sbyte *)
   if (PointerType *PTy = dyn_cast<PointerType>(M->getParamTypes()[0].get()))
-    if (const ArrayType *ATy = dyn_cast<ArrayType>(PTy->getValueType())) {
+    if (PTy->getValueType() == Type::SByteTy ||
+        isa<ArrayType>(PTy->getValueType())) {
       return lle_VP_printstr(M, ArgVal);
     }
 
@@ -132,4 +133,9 @@ GenericValue lle_VB_putchar(MethodType *M, const vector<GenericValue> &Args) {
   return GenericValue();
 }
 
+// void "__main"()
+GenericValue lle_V___main(MethodType *M, const vector<GenericValue> &Args) {
+  return GenericValue();
+}
+
 } // End extern "C"
index 82e7f1fc3fab906faca5adc679b7f68c73d0bc13..8c2f32566a509794359857793a2d77e99cf1a764 100644 (file)
@@ -86,6 +86,9 @@ public:
   static void print(const Type *Ty, GenericValue V);
   static void printValue(const Type *Ty, GenericValue V);
 
+  // Hack until we can parse command line args...
+  bool callMainMethod(const string &MainName,
+                      const string &InputFilename);
 
   void list();             // Do the 'list' command
   void printStackTrace();  // Do the 'backtrace' command
index 4c2faf6ca166016e51583d68794a21a3dba6c8fd..f579ef1a9b4059cbc61fd4182911a94f3d0820ae 100644 (file)
@@ -7,6 +7,7 @@
 #include "Interpreter.h"
 #include "llvm/Bytecode/Reader.h"
 #include "llvm/Assembly/Writer.h"
+#include "llvm/DerivedTypes.h"
 #include <algorithm>
 
 enum CommandID {
@@ -199,3 +200,63 @@ bool Interpreter::callMethod(const string &Name) {
 
   return false;
 }
+
+
+// callMainMethod - This is a nasty gross hack that will dissapear when
+// callMethod can parse command line options and stuff for us.
+//
+bool Interpreter::callMainMethod(const string &Name,
+                                 const string &InputFilename) {
+  vector<Value*> Options = LookupMatchingNames(Name);
+
+  for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
+    if (!isa<Method>(Options[i])) {
+      Options.erase(Options.begin()+i);
+      --i;
+    }
+  }
+
+  Value *PickedMeth = ChooseOneOption(Name, Options);
+  if (PickedMeth == 0)
+    return true;
+
+  Method *M = cast<Method>(PickedMeth);
+  const MethodType *MT = M->getMethodType();
+
+  vector<GenericValue> Args;
+  switch (MT->getParamTypes().size()) {
+  default:
+    cout << "Unknown number of arguments to synthesize for '" << Name << "'!\n";
+    return true;
+  case 2: {
+    PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
+    if (MT->getParamTypes()[1] != SPP) {
+      cout << "Second argument of '" << Name << "' should have type: '"
+           << SPP->getDescription() << "'!\n";
+      return true;
+    }
+    // TODO:
+    GenericValue GV; GV.PointerVal = 0;
+    Args.push_back(GV);
+  }
+    // fallthrough
+  case 1:
+    if (!MT->getParamTypes()[0]->isIntegral()) {
+      cout << "First argument of '" << Name << "' should be integral!\n";
+      return true;
+    } else {
+      GenericValue GV; GV.IntVal = 1;
+      Args.insert(Args.begin(), GV);
+    }
+    // fallthrough
+  case 0:
+    break;
+  }
+
+  callMethod(M, Args);  // Start executing it...
+
+  // Reset the current frame location to the top of stack
+  CurFrame = ECStack.size()-1;
+
+  return false;
+}
index 9bb514e54e7c77cb96ccbe66f492d2d02c12e164..818931c5f79ba029f2a32d8846e9872cbc394896 100644 (file)
@@ -49,7 +49,7 @@ int main(int argc, char** argv) {
 
   // Start interpreter into the main function...
   //
-  if (!I.callMethod(MainFunction) && !DebugMode) {
+  if (!I.callMainMethod(MainFunction, InputFilename) && !DebugMode) {
     // If not in debug mode and if the call succeeded, run the code now...
     I.run();
   }