Extract lookup, getFunctions, getFunctionBinary out of Compiler.
diff --git a/lib/bcc/CodeEmitter.cpp b/lib/bcc/CodeEmitter.cpp
index 78b7bd6..f67b8e5 100644
--- a/lib/bcc/CodeEmitter.cpp
+++ b/lib/bcc/CodeEmitter.cpp
@@ -19,6 +19,7 @@
 #include "CodeMemoryManager.h"
 #include "EmittedFuncInfo.h"
 #include "Runtime.h"
+#include "ScriptCompiled.h"
 
 #include <bcc/bcc.h>
 #include <bcc/bcc_cache.h>
@@ -107,8 +108,9 @@
 namespace bcc {
 
 // Will take the ownership of @MemMgr
-CodeEmitter::CodeEmitter(CodeMemoryManager *pMemMgr)
-    : mpMemMgr(pMemMgr),
+CodeEmitter::CodeEmitter(ScriptCompiled *result, CodeMemoryManager *pMemMgr)
+    : mpResult(result),
+      mpMemMgr(pMemMgr),
       mpTarget(NULL),
       mpTJI(NULL),
       mpTD(NULL),
@@ -156,20 +158,14 @@
 void CodeEmitter::reset() {
   releaseUnnecessary();
 
+  mpResult = NULL;
+
   mpSymbolLookupFn = NULL;
   mpSymbolLookupContext = NULL;
 
   mpTJI = NULL;
   mpTD = NULL;
 
-  for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(),
-          E = mEmittedFunctions.end();
-       I != E;
-       I++)
-    if (I->second != NULL)
-      delete I->second;
-  mEmittedFunctions.clear();
-
   mpMemMgr->reset();
 }
 
@@ -1385,9 +1381,11 @@
   mpCurEmitFunction->Size = CurBufferPtr - BufferBegin;
   BufferBegin = CurBufferPtr = 0;
 
-  if (F.getFunction()->hasName())
-    mEmittedFunctions[F.getFunction()->getNameStr()] = mpCurEmitFunction;
-  mpCurEmitFunction = NULL;
+  if (F.getFunction()->hasName()) {
+    string const &name = F.getFunction()->getNameStr();
+    mpResult->mEmittedFunctions[name] = mpCurEmitFunction;
+    mpCurEmitFunction = NULL;
+  }
 
   mRelocations.clear();
   mConstPoolAddresses.clear();
@@ -1531,43 +1529,4 @@
 }
 
 
-void *CodeEmitter::lookup(const llvm::StringRef &Name) {
-  EmittedFunctionsMapTy::const_iterator
-    I = mEmittedFunctions.find(Name.str());
-
-  return (I == mEmittedFunctions.end()) ? NULL : I->second->Code;
-}
-
-
-void CodeEmitter::getFunctionNames(BCCsizei *actualFunctionCount,
-                                   BCCsizei maxFunctionCount,
-                                   BCCchar **functions) {
-  int functionCount = mEmittedFunctions.size();
-
-  if (actualFunctionCount)
-    *actualFunctionCount = functionCount;
-  if (functionCount > maxFunctionCount)
-    functionCount = maxFunctionCount;
-  if (functions)
-    for (EmittedFunctionsMapTy::const_iterator
-         I = mEmittedFunctions.begin(), E = mEmittedFunctions.end();
-         I != E && (functionCount > 0); I++, functionCount--) {
-      *functions++ = const_cast<BCCchar*>(I->first.c_str());
-    }
-}
-
-
-void CodeEmitter::getFunctionBinary(BCCchar *label,
-                                    BCCvoid **base,
-                                    BCCsizei *length) {
-  EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(label);
-  if (I == mEmittedFunctions.end()) {
-    *base = NULL;
-    *length = 0;
-  } else {
-    *base = I->second->Code;
-    *length = I->second->Size;
-  }
-}
-
 } // namespace bcc
diff --git a/lib/bcc/CodeEmitter.h b/lib/bcc/CodeEmitter.h
index 2aeeef2..72d9665 100644
--- a/lib/bcc/CodeEmitter.h
+++ b/lib/bcc/CodeEmitter.h
@@ -61,15 +61,13 @@
 namespace bcc {
   class CodeMemoryManager;
   class EmittedFuncInfo;
+  class ScriptCompiled;
 
   class CodeEmitter : public llvm::JITCodeEmitter {
   private:
     typedef llvm::DenseMap<const llvm::GlobalValue *, void *>
       GlobalAddressMapTy;
 
-    typedef std::map<const std::string, EmittedFuncInfo *>
-      EmittedFunctionsMapTy;
-
     typedef llvm::DenseMap<const llvm::Function *, void*>
       FunctionToLazyStubMapTy;
 
@@ -81,6 +79,8 @@
 
 
   private:
+    ScriptCompiled *mpResult;
+
     CodeMemoryManager *mpMemMgr;
 
     // The JITInfo for the target we are compiling to
@@ -93,8 +93,6 @@
 
     EmittedFuncInfo *mpCurEmitFunction;
 
-    EmittedFunctionsMapTy mEmittedFunctions;
-
     GlobalAddressMapTy mGlobalAddressMap;
 
     // This vector is a mapping from MBB ID's to their address. It is filled in
@@ -153,7 +151,7 @@
     void *mpSymbolLookupContext;
 
     // Will take the ownership of @MemMgr
-    explicit CodeEmitter(CodeMemoryManager *pMemMgr);
+    explicit CodeEmitter(ScriptCompiled *result, CodeMemoryManager *pMemMgr);
 
     virtual ~CodeEmitter();
 
@@ -249,20 +247,6 @@
 
     void reset();
 
-    void *lookup(const char *Name) {
-      return lookup( llvm::StringRef(Name) );
-    }
-
-    void *lookup(const llvm::StringRef &Name);
-
-    void getFunctionNames(BCCsizei *actualFunctionCount,
-                          BCCsizei maxFunctionCount,
-                          BCCchar **functions);
-
-    void getFunctionBinary(BCCchar *label,
-                           BCCvoid **base,
-                           BCCsizei *length);
-
   private:
     void startGVStub(const llvm::GlobalValue *GV, unsigned StubSize,
                      unsigned Alignment);
diff --git a/lib/bcc/Compiler.cpp b/lib/bcc/Compiler.cpp
index 41dc020..f131023 100644
--- a/lib/bcc/Compiler.cpp
+++ b/lib/bcc/Compiler.cpp
@@ -303,7 +303,7 @@
 
 
 CodeEmitter *Compiler::createCodeEmitter() {
-  mCodeEmitter.reset(new CodeEmitter(mCodeMemMgr.get()));
+  mCodeEmitter.reset(new CodeEmitter(mpResult, mCodeMemMgr.get()));
   return mCodeEmitter.get();
 }
 
@@ -1019,7 +1019,7 @@
         if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
           llvm::StringRef ExportFuncName =
             static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
-          funcList.push_back(mCodeEmitter->lookup(ExportFuncName));
+          funcList.push_back(mpResult->lookup(ExportFuncName.str().c_str()));
         }
       }
     }
@@ -1083,53 +1083,6 @@
 }
 
 
-// interface for bccGetScriptLabel()
-void *Compiler::lookup(const char *name) {
-  void *addr = NULL;
-  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
-    if (!strcmp(name, "root")) {
-      addr = reinterpret_cast<void *>(mCacheHdr->rootAddr);
-    } else if (!strcmp(name, "init")) {
-      addr = reinterpret_cast<void *>(mCacheHdr->initAddr);
-    }
-    return addr;
-  }
-
-  if (mCodeEmitter.get())
-    // Find function pointer
-    addr = mCodeEmitter->lookup(name);
-  return addr;
-}
-
-// Interface for bccGetFunctions()
-void Compiler::getFunctions(BCCsizei *actualFunctionCount,
-                            BCCsizei maxFunctionCount,
-                            BCCchar **functions) {
-  if (mCodeEmitter.get())
-    mCodeEmitter->getFunctionNames(actualFunctionCount,
-                                   maxFunctionCount,
-                                   functions);
-  else
-    *actualFunctionCount = 0;
-
-  return;
-}
-
-
-// Interface for bccGetFunctionBinary()
-void Compiler::getFunctionBinary(BCCchar *function,
-                                 BCCvoid **base,
-                                 BCCsizei *length) {
-  if (mCodeEmitter.get()) {
-    mCodeEmitter->getFunctionBinary(function, base, length);
-  } else {
-    *base = NULL;
-    *length = 0;
-  }
-  return;
-}
-
-
 Compiler::~Compiler() {
   if (!mCodeMemMgr.get()) {
     // mCodeDataAddr and mCacheMapAddr are from loadCacheFile and not
@@ -1260,8 +1213,8 @@
 
   // Current Memory Address (Saved for Recalculation)
   hdr->cachedCodeDataAddr = reinterpret_cast<uint32_t>(mCodeDataAddr);
-  hdr->rootAddr = reinterpret_cast<uint32_t>(lookup("root"));
-  hdr->initAddr = reinterpret_cast<uint32_t>(lookup("init"));
+  hdr->rootAddr = reinterpret_cast<uint32_t>(mpResult->lookup("root"));
+  hdr->initAddr = reinterpret_cast<uint32_t>(mpResult->lookup("init"));
 
   // Check libRS isThreadable
   if (!mCodeEmitter) {
diff --git a/lib/bcc/Compiler.h b/lib/bcc/Compiler.h
index cf9623e..d3bfb5f 100644
--- a/lib/bcc/Compiler.h
+++ b/lib/bcc/Compiler.h
@@ -149,34 +149,6 @@
       return mError.c_str();
     }
 
-    // interface for bccGetScriptLabel()
-    void *lookup(const char *name);
-
-    // Interface for bccGetExportVars()
-    void getExportVars(BCCsizei *actualVarCount,
-                       BCCsizei maxVarCount,
-                       BCCvoid **vars);
-
-    // Interface for bccGetExportFuncs()
-    void getExportFuncs(BCCsizei *actualFuncCount,
-                        BCCsizei maxFuncCount,
-                        BCCvoid **funcs);
-
-    // Interface for bccGetPragmas()
-    void getPragmas(BCCsizei *actualStringCount,
-                    BCCsizei maxStringCount,
-                    BCCchar **strings);
-
-    // Interface for bccGetFunctions()
-    void getFunctions(BCCsizei *actualFunctionCount,
-                      BCCsizei maxFunctionCount,
-                      BCCchar **functions);
-
-    // Interface for bccGetFunctionBinary()
-    void getFunctionBinary(BCCchar *function,
-                           BCCvoid **base,
-                           BCCsizei *length);
-
     const llvm::Module *getModule() const {
       return mModule;
     }
diff --git a/lib/bcc/ScriptCompiled.cpp b/lib/bcc/ScriptCompiled.cpp
index 1611901..b34e949 100644
--- a/lib/bcc/ScriptCompiled.cpp
+++ b/lib/bcc/ScriptCompiled.cpp
@@ -19,8 +19,21 @@
 
 #include "ScriptCompiled.h"
 
+#include "EmittedFuncInfo.h"
+
 namespace bcc {
 
+ScriptCompiled::~ScriptCompiled() {
+  for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(),
+       E = mEmittedFunctions.end(); I != E; I++) {
+    if (I->second != NULL) {
+      delete I->second;
+    }
+  }
+
+  mEmittedFunctions.clear();
+}
+
 void ScriptCompiled::getExportVars(BCCsizei *actualVarCount,
                                    BCCsizei maxVarCount,
                                    BCCvoid **vars) {
@@ -147,4 +160,56 @@
   }
 }
 
+
+void *ScriptCompiled::lookup(const char *name) {
+#if 0
+  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
+    if (!strcmp(name, "root")) {
+      addr = reinterpret_cast<void *>(mCacheHdr->rootAddr);
+    } else if (!strcmp(name, "init")) {
+      addr = reinterpret_cast<void *>(mCacheHdr->initAddr);
+    }
+    return addr;
+  }
+#endif
+
+  EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(name);
+  return (I == mEmittedFunctions.end()) ? NULL : I->second->Code;
+}
+
+
+void ScriptCompiled::getFunctions(BCCsizei *actualFunctionCount,
+                                  BCCsizei maxFunctionCount,
+                                  BCCchar **functions) {
+
+  int functionCount = mEmittedFunctions.size();
+
+  if (actualFunctionCount)
+    *actualFunctionCount = functionCount;
+  if (functionCount > maxFunctionCount)
+    functionCount = maxFunctionCount;
+  if (functions) {
+    for (EmittedFunctionsMapTy::const_iterator
+         I = mEmittedFunctions.begin(), E = mEmittedFunctions.end();
+         I != E && (functionCount > 0); I++, functionCount--) {
+      *functions++ = const_cast<BCCchar*>(I->first.c_str());
+    }
+  }
+}
+
+
+void ScriptCompiled::getFunctionBinary(BCCchar *funcname,
+                                       BCCvoid **base,
+                                       BCCsizei *length) {
+  EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(funcname);
+  if (I == mEmittedFunctions.end()) {
+    *base = NULL;
+    *length = 0;
+  } else {
+    *base = I->second->Code;
+    *length = I->second->Size;
+  }
+}
+
+
 } // namespace bcc
diff --git a/lib/bcc/ScriptCompiled.h b/lib/bcc/ScriptCompiled.h
index e9c1fa2..e271042 100644
--- a/lib/bcc/ScriptCompiled.h
+++ b/lib/bcc/ScriptCompiled.h
@@ -21,20 +21,29 @@
 
 #include <bcc/bcc.h>
 
+#include <list>
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
+
 namespace llvm {
   class Module;
 }
 
 namespace bcc {
+  class EmittedFuncInfo;
   class Script;
 
   class ScriptCompiled {
     friend class Compiler;
+    friend class CodeEmitter;
 
   private:
-    typedef std::list< std::pair<std::string, std::string> > PragmaList;
+    typedef std::list<std::pair<std::string, std::string> > PragmaList;
     typedef std::list<void*> ExportVarList;
     typedef std::list<void*> ExportFuncList;
+    typedef std::map<std::string, EmittedFuncInfo *> EmittedFunctionsMapTy;
 
   private:
     Script *mpOwner;
@@ -44,11 +53,14 @@
     PragmaList mPragmas;
     ExportVarList mExportVars;
     ExportFuncList mExportFuncs;
+    EmittedFunctionsMapTy mEmittedFunctions;
 
   public:
     ScriptCompiled(Script *owner) : mpOwner(owner), mCompiler(this) {
     }
 
+    ~ScriptCompiled();
+
     int readBC(const char *bitcode,
                size_t bitcodeSize,
                long bitcodeFileModTime,
@@ -76,9 +88,7 @@
       return mCompiler.getErrorMessage();
     }
 
-    void *lookup(const char *name) {
-      return mCompiler.lookup(name);
-    }
+    void *lookup(const char *name);
 
     void getExportVars(BCCsizei *actualVarCount,
                        BCCsizei maxVarCount,
@@ -94,15 +104,11 @@
 
     void getFunctions(BCCsizei *actualFunctionCount,
                       BCCsizei maxFunctionCount,
-                      BCCchar **functions) {
-      mCompiler.getFunctions(actualFunctionCount, maxFunctionCount, functions);
-    }
+                      BCCchar **functions);
 
     void getFunctionBinary(BCCchar *function,
                            BCCvoid **base,
-                           BCCsizei *length) {
-      mCompiler.getFunctionBinary(function, base, length);
-    }
+                           BCCsizei *length);
 
     void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
       mCompiler.registerSymbolCallback(pFn, pContext);