Integrate from jush  MC Assembler is done.

Change-Id: I5e640691397b8988a6b27fb454c4bfc24a4f5c41
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
index 4eaf87d..2fafa3d 100644
--- a/lib/ExecutionEngine/Compiler.cpp
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -44,6 +44,7 @@
 #include "llvm/Target/TargetSelect.h"
 
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/MemoryBuffer.h"
 
 #include "llvm/GlobalValue.h"
@@ -243,6 +244,32 @@
 }
 
 
+int Compiler::getFilePath(char *filePath) {
+  char outStr[256];
+  char *replaceStr;
+
+  strcpy(outStr, mResName);
+  LOGE("outStr = %s\n", outStr);
+
+  // For example, if mResName is "com.android.balls:raw/balls",
+  // filePath will be "/data/data/com.android.balls/ELF_balls.o".
+  //
+  replaceStr = strstr(outStr, ":raw/");
+  if (replaceStr) {
+    strncpy(replaceStr, "_ELF_", 5);
+  } else {
+    LOGE("Ill formatted resource name\n");
+    return 1;
+  }
+
+  strcpy(filePath, "/data/local/tmp/");
+  strcat(filePath, outStr);
+  strcat(filePath, ".o");
+
+  return 0;
+}
+
+
 llvm::Module *Compiler::parseBitcodeFile(llvm::MemoryBuffer *MEM) {
   llvm::Module *result = llvm::ParseBitcodeFile(MEM, *mContext, &mError);
 
@@ -273,7 +300,33 @@
   const llvm::Target *Target;
   std::string FeaturesStr;
 
+#if CLASSIC_JIT
   llvm::FunctionPassManager *CodeGenPasses = NULL;
+#endif
+
+#if MC_ASSEMBLER
+  bool RelaxAll = true;
+  llvm::PassManager MCCodeGenPasses;
+
+  char ObjectPath[512];
+  if (getFilePath(ObjectPath)) {
+    LOGE("Fail to create ObjectPath");
+    return 1;
+  }
+
+  int Fd = -1;
+  Fd = open(ObjectPath, O_CREAT | O_RDWR | O_TRUNC);
+  //            S_IRUSR , S_IWUSR , S_IRGRP , S_IROTH /* 0644 */);
+
+  if (Fd < 0) {
+    LOGE("Fail to open file '%s'", ObjectPath);
+    return 1;
+  }
+
+  llvm::raw_fd_ostream OutFdOS(Fd, /* shouldClose= */ false);
+  OutFdOS.seek(0);
+  llvm::formatted_raw_ostream OutFOS(OutFdOS);
+#endif
 
   const llvm::NamedMDNode *PragmaMetadata;
   const llvm::NamedMDNode *ExportVarMetadata;
@@ -465,6 +518,7 @@
     LTOPasses.run(*mModule);
   }
 
+#if CLASSIC_JIT
   // Create code-gen pass to run the code emitter
   CodeGenPasses = new llvm::FunctionPassManager(mModule);
   CodeGenPasses->add(TD);  // Will take the ownership of TD
@@ -488,7 +542,22 @@
   }
 
   CodeGenPasses->doFinalization();
+#endif
 
+#if MC_ASSEMBLER
+  TM->setMCRelaxAll(RelaxAll);
+
+  MCCodeGenPasses.add(new llvm::TargetData(*TD));
+
+  if (TM->addPassesToEmitFile(MCCodeGenPasses, OutFOS,
+                              llvm::TargetMachine::CGFT_ObjectFile,
+                              CodeGenOptLevel)) {
+    setError("Fail to add passes to emit file");
+    goto on_bcc_compile_error;
+  }
+
+  MCCodeGenPasses.run(*mModule);
+#endif
   // Copy the global address mapping from code emitter and remapping
   if (ExportVarMetadata) {
     ScriptCompiled::ExportVarList &varList = mpResult->mExportVars;
diff --git a/lib/ExecutionEngine/Compiler.h b/lib/ExecutionEngine/Compiler.h
index a22c329..ba32a28 100644
--- a/lib/ExecutionEngine/Compiler.h
+++ b/lib/ExecutionEngine/Compiler.h
@@ -79,6 +79,8 @@
 
 
   private:
+    const char *mResName;
+
     ScriptCompiled *mpResult;
 
     std::string mError;
@@ -102,6 +104,11 @@
 
     static void GlobalInitialization();
 
+    void setResName(const char *resName) {
+      mResName = resName;
+      return;
+    }
+
     void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
       mpSymbolLookupFn = pFn;
       mpSymbolLookupContext = pContext;
@@ -111,6 +118,8 @@
 
     CodeEmitter *createCodeEmitter();
 
+    int getFilePath(char *ObjectPath);
+
     llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
 
     int readModule(llvm::Module *module) {
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
index cc9801a..d43d9cf 100644
--- a/lib/ExecutionEngine/Script.cpp
+++ b/lib/ExecutionEngine/Script.cpp
@@ -76,6 +76,15 @@
                         const char *bitcode,
                         size_t bitcodeSize,
                         unsigned long flags) {
+
+  if (!resName) {
+    mErrorCode = BCC_INVALID_VALUE;
+    LOGE("Invalid argument: resName = NULL\n");
+    return 1;
+  }
+
+  mResName = resName;
+
   if (mStatus != ScriptStatus::Unknown) {
     mErrorCode = BCC_INVALID_OPERATION;
     LOGE("Bad operation: Adding source after bccPrepareExecutable\n");
@@ -289,6 +298,8 @@
     }
   }
 
+  mCompiled->setResName(mResName);
+
   // Compile and JIT the code
   if (mCompiled->compile() != 0) {
     LOGE("Unable to compile.\n");
diff --git a/lib/ExecutionEngine/Script.h b/lib/ExecutionEngine/Script.h
index dc115da..a5e2bad 100644
--- a/lib/ExecutionEngine/Script.h
+++ b/lib/ExecutionEngine/Script.h
@@ -58,6 +58,8 @@
 
     char const *mCachePath;
 
+    char const *mResName;
+
     bool mIsContextSlotNotAvail;
 
     // Source List
diff --git a/lib/ExecutionEngine/ScriptCompiled.h b/lib/ExecutionEngine/ScriptCompiled.h
index d2c7773..aa8e4cc 100644
--- a/lib/ExecutionEngine/ScriptCompiled.h
+++ b/lib/ExecutionEngine/ScriptCompiled.h
@@ -66,6 +66,11 @@
 
     ~ScriptCompiled();
 
+    void setResName(char const *resName) {
+      mCompiler.setResName(resName);
+      return;
+    }
+
     llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM) {
       return mCompiler.parseBitcodeFile(MEM);
     }