Refactoring.

Change-Id: I283837f0adc00ba328e981f419483a0defb22f65
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
index 9e59495..bfa914b 100644
--- a/lib/ExecutionEngine/Script.cpp
+++ b/lib/ExecutionEngine/Script.cpp
@@ -183,6 +183,7 @@
                           char const *cacheName,
                           llvm::Reloc::Model RelocModel,
                           unsigned long flags) {
+  mObjectType = ScriptObject::Relocatable;
 #if USE_CACHE
   if (cacheDir && cacheName) {
     // Set Cache Directory and File Name
@@ -220,6 +221,7 @@
     return 1;
   }
 
+  mObjectType = ScriptObject::Executable;
 #if USE_CACHE
   if (cacheDir && cacheName) {
     // Set Cache Directory and File Name
@@ -261,13 +263,8 @@
     return 1;
   }
 
-#if USE_OLD_JIT
-  std::string objPath(mCacheDir + mCacheName + ".jit-image");
-  std::string infoPath(mCacheDir + mCacheName + ".oBCC"); // TODO: .info instead
-#elif USE_MCJIT
-  std::string objPath(mCacheDir + mCacheName + ".o");
-  std::string infoPath(mCacheDir + mCacheName + ".info");
-#endif
+  std::string objPath = getCachedObjectPath();
+  std::string infoPath = getCacheInfoPath();
 
   FileHandle objFile;
   if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
@@ -390,14 +387,8 @@
 #endif
       !getBooleanProp("debug.bcc.nocache")) {
 
-#if USE_OLD_JIT
-    std::string objPath(mCacheDir + mCacheName + ".jit-image");
-    std::string infoPath(mCacheDir + mCacheName + ".oBCC");
-#elif USE_MCJIT
-    std::string objPath(mCacheDir + mCacheName + ".o");
-    std::string infoPath(mCacheDir + mCacheName + ".info");
-#endif
-
+    std::string objPath = getCachedObjectPath();
+    std::string infoPath = getCacheInfoPath();
 
     // Remove the file if it already exists before writing the new file.
     // The old file may still be mapped elsewhere in memory and we do not want
diff --git a/lib/ExecutionEngine/Script.h b/lib/ExecutionEngine/Script.h
index 9138db5..bb4ace3 100644
--- a/lib/ExecutionEngine/Script.h
+++ b/lib/ExecutionEngine/Script.h
@@ -49,11 +49,20 @@
     };
   }
 
+  namespace ScriptObject {
+    enum ObjectType {
+      Unknown,
+      Relocatable,
+      Executable,
+    };
+  }
+
   class Script {
   private:
     int mErrorCode;
 
     ScriptStatus::StatusType mStatus;
+    ScriptObject::ObjectType mObjectType;
 
     union {
       ScriptCompiled *mCompiled;
@@ -65,6 +74,35 @@
 #if USE_CACHE
     std::string mCacheDir;
     std::string mCacheName;
+
+    inline std::string getCachedObjectPath() const {
+#if USE_OLD_JIT
+      return std::string(mCacheDir + mCacheName + ".jit-image");
+#elif USE_MCJIT
+      std::string objPath(mCacheDir + mCacheName);
+
+      // Append suffix depends on the object type
+      switch (mObjectType) {
+        case ScriptObject::Relocatable:
+        case ScriptObject::Executable: {
+          objPath.append(".o");
+          break;
+        }
+        default: {
+          assert(false && "Unknown onject type!");
+        }
+      }
+      return objPath;
+#endif
+    }
+
+    inline std::string getCacheInfoPath() const {
+#if USE_OLD_JIT
+      return std::string(mCacheDir + mCacheName + ".oBCC");
+#elif USE_MCJIT
+      return std::string(mCacheDir + mCacheName + ".info");
+#endif
+    }
 #endif
 
     bool mIsContextSlotNotAvail;
@@ -84,7 +122,7 @@
 
   public:
     Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
-               mIsContextSlotNotAvail(false),
+               mObjectType(ScriptObject::Unknown), mIsContextSlotNotAvail(false),
                mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
       Compiler::GlobalInitialization();