Added the skeleton of an IR transformer that will
prepare IR for execution in the target.  Wired the
expression command to use this IR transformer when
conversion to DWARF fails, and wired conversion to
DWARF to always fail (well, we don't generate any
DWARF...)


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@107559 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ClangExpression.cpp b/source/Expression/ClangExpression.cpp
index 6922ac4..a3a58b6 100644
--- a/source/Expression/ClangExpression.cpp
+++ b/source/Expression/ClangExpression.cpp
@@ -58,6 +58,7 @@
 #include "lldb/Expression/ClangASTSource.h"
 #include "lldb/Expression/ClangResultSynthesizer.h"
 #include "lldb/Expression/ClangStmtVisitor.h"
+#include "lldb/Expression/IRForTarget.h"
 #include "lldb/Expression/IRToDWARF.h"
 #include "lldb/Symbol/ClangASTContext.h"
 #include "lldb/Expression/RecordingMemoryManager.h"
@@ -474,7 +475,7 @@
     return 0;
 }
 
-unsigned
+bool
 ClangExpression::ConvertIRToDWARF (ClangExpressionVariableList &expr_local_variable_list,
                                    StreamString &dwarf_opcode_strm)
 {
@@ -496,6 +497,26 @@
 }
 
 bool
+ClangExpression::PrepareIRForTarget (ClangExpressionVariableList &expr_local_variable_list)
+{
+    Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
+    
+    llvm::Module *module = m_code_generator_ptr->GetModule();
+    
+    if (!module)
+    {
+        if (log)
+            log->Printf("IR doesn't contain a module");
+        
+        return 1;
+    }
+    
+    IRForTarget ir_for_target("IR for target", m_decl_map);
+    
+    return ir_for_target.runOnModule(*module);
+}
+
+bool
 ClangExpression::JITFunction (const ExecutionContext &exc_context, const char *name)
 {
 
diff --git a/source/Expression/IRForTarget.cpp b/source/Expression/IRForTarget.cpp
new file mode 100644
index 0000000..a6b50e5
--- /dev/null
+++ b/source/Expression/IRForTarget.cpp
@@ -0,0 +1,109 @@
+//===-- IRForTarget.cpp -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Expression/IRForTarget.h"
+
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/InstrTypes.h"
+#include "llvm/Module.h"
+
+#include "lldb/Core/dwarf.h"
+#include "lldb/Core/Log.h"
+#include "lldb/Core/Scalar.h"
+#include "lldb/Core/StreamString.h"
+#include "lldb/Expression/ClangExpressionDeclMap.h"
+
+#include <map>
+
+using namespace llvm;
+
+IRForTarget::IRForTarget(const void *pid,
+                         lldb_private::ClangExpressionDeclMap *decl_map) :
+    ModulePass(pid),
+    m_decl_map(decl_map)
+{
+}
+
+IRForTarget::~IRForTarget()
+{
+}
+
+bool
+IRForTarget::runOnBasicBlock(BasicBlock &BB)
+{
+    lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
+        
+    /////////////////////////////////////////////////////////////////////////
+    // Prepare the current basic block for execution in the remote process
+    //
+    
+    if (log)
+    {
+        log->Printf("Preparing basic block %s:",
+                    BB.hasName() ? BB.getNameStr().c_str() : "[anonymous]");
+        
+        llvm::BasicBlock::iterator ii;
+        
+        for (ii = BB.begin();
+             ii != BB.end();
+             ++ii)
+        {
+            llvm::Instruction &inst = *ii;
+            
+            std::string s;
+            raw_string_ostream os(s);
+            
+            inst.print(os);
+            
+            if (log)
+                log->Printf("  %s", s.c_str());
+        }
+    }
+    
+    return true;
+}
+
+bool
+IRForTarget::runOnModule(Module &M)
+{
+    lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
+    
+    llvm::Function* function = M.getFunction(StringRef("___clang_expr"));
+    
+    if (!function)
+    {
+        if (log)
+            log->Printf("Couldn't find ___clang_expr() in the module");
+        
+        return false;
+    }
+        
+    llvm::Function::iterator bbi;
+    
+    for (bbi = function->begin();
+         bbi != function->end();
+         ++bbi)
+    {
+        runOnBasicBlock(*bbi);
+    }
+    
+    return true;    
+}
+
+void
+IRForTarget::assignPassManager(PMStack &PMS,
+                             PassManagerType T)
+{
+}
+
+PassManagerType
+IRForTarget::getPotentialPassManagerType() const
+{
+    return PMT_ModulePassManager;
+}
diff --git a/source/Expression/IRToDWARF.cpp b/source/Expression/IRToDWARF.cpp
index 0cca9aa..af6d1b1 100644
--- a/source/Expression/IRToDWARF.cpp
+++ b/source/Expression/IRToDWARF.cpp
@@ -207,9 +207,11 @@
     
     Relocator relocator;
     
-    llvm::BasicBlock &currentBB = function->getEntryBlock();
+    if (!runOnBasicBlock(function->getEntryBlock(), relocator))
+        return false;
     
-    runOnBasicBlock(currentBB, relocator);
+    // TEMPORARY: Fail in order to force execution in the target.
+    return false;
     
     return relocator.ResolveRelocations(m_strm);    
 }