On Linux platforms and at optimization levels -O1 and above, llvm-gcc can
authorReid Spencer <rspencer@reidspencer.com>
Sat, 19 May 2007 01:36:17 +0000 (01:36 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sat, 19 May 2007 01:36:17 +0000 (01:36 +0000)
turn "putchar" calls into _IO_putc calls which is a lower-level interface.
This patch allows these calls to be executed by lli in interpreter mode.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37254 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ExecutionEngine/Interpreter/ExternalFunctions.cpp

index 53f5deb13a2babb3d55ec1bd1b334d089eabd534..14dcdf9e0364595cab0c8d5d60dbcdf3a8a69b6e 100644 (file)
@@ -71,14 +71,14 @@ static ExFunc lookupFunction(const Function *F) {
   ExtName += "_" + F->getName();
 
   ExFunc FnPtr = FuncNames[ExtName];
-  if (FnPtr == 0)
-    FnPtr = 
-      (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(ExtName);
   if (FnPtr == 0)
     FnPtr = FuncNames["lle_X_"+F->getName()];
   if (FnPtr == 0)  // Try calling a generic function... if it exists...
     FnPtr = (ExFunc)(intptr_t)sys::DynamicLibrary::SearchForAddressOfSymbol(
             ("lle_X_"+F->getName()).c_str());
+  if (FnPtr == 0)
+    FnPtr = (ExFunc)(intptr_t)
+      sys::DynamicLibrary::SearchForAddressOfSymbol(F->getName());
   if (FnPtr != 0)
     Functions.insert(std::make_pair(F, FnPtr));  // Cache for later
   return FnPtr;
@@ -118,6 +118,16 @@ GenericValue lle_X_putchar(FunctionType *FT, const vector<GenericValue> &Args){
   return Args[0];
 }
 
+// void _IO_putc(int c, FILE* fp)
+GenericValue lle_X__IO_putc(FunctionType *FT, const vector<GenericValue> &Args){
+#ifdef __linux__
+  _IO_putc((char)Args[0].IntVal.getZExtValue(), (FILE*) Args[1].PointerVal);
+#else
+  assert(0 && "Can't call _IO_putc on this platform");
+#endif
+  return Args[0];
+}
+
 // void atexit(Function*)
 GenericValue lle_X_atexit(FunctionType *FT, const vector<GenericValue> &Args) {
   assert(Args.size() == 1);
@@ -694,6 +704,7 @@ GenericValue lle_X_fprintf(FunctionType *FT, const vector<GenericValue> &Args) {
 
 void Interpreter::initializeExternalFunctions() {
   FuncNames["lle_X_putchar"]      = lle_X_putchar;
+  FuncNames["lle_X__IO_putc"]     = lle_X__IO_putc;
   FuncNames["lle_X_exit"]         = lle_X_exit;
   FuncNames["lle_X_abort"]        = lle_X_abort;
   FuncNames["lle_X_malloc"]       = lle_X_malloc;