<div dir="ltr">Thanks!  IMO this is significantly better than re-including the .inc files out of support.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jan 23, 2014 at 3:04 AM, Alp Toker <span dir="ltr"><<a href="mailto:alp@nuanti.com" target="_blank">alp@nuanti.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: alp<br>
Date: Thu Jan 23 05:04:42 2014<br>
New Revision: 199881<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=199881&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=199881&view=rev</a><br>
Log:<br>
Refactor lli-child-target to remove duplicated code<br>
<br>
Eliminate the copies LLVM's System mmap and cache invalidation code. These were<br>
slowly drifting away from the original version, and moreover the copied code<br>
was a dead end in terms of portability.<br>
<br>
We now statically link to Support but in practice with stripping this adds next<br>
to no weight to the resultant binary.<br>
<br>
Also avoid installing lli-child-target to the user's $PATH. It's not meant to<br>
be run directly.<br>
<br>
Modified:<br>
    llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt<br>
    llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp<br>
    llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc<br>
    llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc<br>
    llvm/trunk/tools/lli/RemoteTarget.cpp<br>
    llvm/trunk/tools/lli/RemoteTarget.h<br>
<br>
Modified: llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt (original)<br>
+++ llvm/trunk/tools/lli/ChildTarget/CMakeLists.txt Thu Jan 23 05:04:42 2014<br>
@@ -1,3 +1,6 @@<br>
-add_llvm_tool(lli-child-target<br>
+set(LLVM_LINK_COMPONENTS support)<br>
+<br>
+add_llvm_executable(lli-child-target<br>
   ChildTarget.cpp<br>
-  )<br>
+  ../RemoteTarget.cpp<br>
+)<br>
<br>
Modified: llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp (original)<br>
+++ llvm/trunk/tools/lli/ChildTarget/ChildTarget.cpp Thu Jan 23 05:04:42 2014<br>
@@ -1,4 +1,6 @@<br>
 #include "llvm/Config/config.h"<br>
+#include "llvm/Support/Memory.h"<br>
+#include "../RemoteTarget.h"<br>
 #include "../RemoteTargetMessage.h"<br>
 #include <assert.h><br>
 #include <map><br>
@@ -14,13 +16,13 @@ public:<br>
   void initialize();<br>
   LLIMessageType waitForIncomingMessage();<br>
   void handleMessage(LLIMessageType messageType);<br>
+  RemoteTarget *RT;<br>
<br>
 private:<br>
   // Incoming message handlers<br>
   void handleAllocateSpace();<br>
   void handleLoadSection(bool IsCode);<br>
   void handleExecute();<br>
-  void handleTerminate();<br>
<br>
   // Outgoing message handlers<br>
   void sendChildActive();<br>
@@ -32,15 +34,6 @@ private:<br>
   void initializeConnection();<br>
   int WriteBytes(const void *Data, size_t Size);<br>
   int ReadBytes(void *Data, size_t Size);<br>
-  uint64_t allocate(uint32_t Alignment, uint32_t Size);<br>
-  void makeSectionExecutable(uint64_t Addr, uint32_t Size);<br>
-  void InvalidateInstructionCache(const void *Addr, size_t Len);<br>
-  void releaseMemory(uint64_t Addr, uint32_t Size);<br>
-  bool isAllocatedMemory(uint64_t Address, uint32_t Size);<br>
-<br>
-  // Store a map of allocated buffers to sizes.<br>
-  typedef std::map<uint64_t, uint32_t> AllocMapType;<br>
-  AllocMapType m_AllocatedBufferMap;<br>
<br>
   // Communication handles (OS-specific)<br>
   void *ConnectionData;<br>
@@ -48,6 +41,7 @@ private:<br>
<br>
 int main() {<br>
   LLIChildTarget  ThisChild;<br>
+  ThisChild.RT = new RemoteTarget();<br>
   ThisChild.initialize();<br>
   LLIMessageType MsgType;<br>
   do {<br>
@@ -55,6 +49,7 @@ int main() {<br>
     ThisChild.handleMessage(MsgType);<br>
   } while (MsgType != LLI_Terminate &&<br>
            MsgType != LLI_Error);<br>
+  delete ThisChild.RT;<br>
   return 0;<br>
 }<br>
<br>
@@ -86,7 +81,7 @@ void LLIChildTarget::handleMessage(LLIMe<br>
       handleExecute();<br>
       break;<br>
     case LLI_Terminate:<br>
-      handleTerminate();<br>
+      RT->stop();<br>
       break;<br>
     default:<br>
       // FIXME: Handle error!<br>
@@ -112,7 +107,8 @@ void LLIChildTarget::handleAllocateSpace<br>
   assert(rc == 4);<br>
<br>
   // Allocate the memory.<br>
-  uint64_t Addr = allocate(Alignment, AllocSize);<br>
+  uint64_t Addr;<br>
+  RT->allocateSpace(AllocSize, Alignment, Addr);<br>
<br>
   // Send AllocationResult message.<br>
   sendAllocationResult(Addr);<br>
@@ -131,7 +127,7 @@ void LLIChildTarget::handleLoadSection(b<br>
   assert(rc == 8);<br>
   size_t BufferSize = DataSize - 8;<br>
<br>
-  if (!isAllocatedMemory(Addr, BufferSize))<br>
+  if (!RT->isAllocatedMemory(Addr, BufferSize))<br>
     return sendLoadStatus(LLI_Status_NotAllocated);<br>
<br>
   // Read section data into previously allocated buffer<br>
@@ -141,7 +137,7 @@ void LLIChildTarget::handleLoadSection(b<br>
<br>
   // If IsCode, mark memory executable<br>
   if (IsCode)<br>
-    makeSectionExecutable(Addr, BufferSize);<br>
+    sys::Memory::InvalidateInstructionCache((void *)Addr, BufferSize);<br>
<br>
   // Send MarkLoadComplete message.<br>
   sendLoadStatus(LLI_Status_Success);<br>
@@ -161,38 +157,13 @@ void LLIChildTarget::handleExecute() {<br>
   assert(rc == 8);<br>
<br>
   // Call function<br>
-  int Result;<br>
-  int (*fn)(void) = (int(*)(void))Addr;<br>
-  Result = fn();<br>
+  int32_t Result = -1;<br>
+  RT->executeCode(Addr, Result);<br>
<br>
   // Send ExecutionResult message.<br>
   sendExecutionComplete(Result);<br>
 }<br>
<br>
-void LLIChildTarget::handleTerminate() {<br>
-  // Release all allocated memory<br>
-  AllocMapType::iterator Begin = m_AllocatedBufferMap.begin();<br>
-  AllocMapType::iterator End = m_AllocatedBufferMap.end();<br>
-  for (AllocMapType::iterator It = Begin; It != End; ++It) {<br>
-    releaseMemory(It->first, It->second);<br>
-  }<br>
-  m_AllocatedBufferMap.clear();<br>
-}<br>
-<br>
-bool LLIChildTarget::isAllocatedMemory(uint64_t Address, uint32_t Size) {<br>
-  uint64_t End = Address+Size;<br>
-  AllocMapType::iterator ItBegin = m_AllocatedBufferMap.begin();<br>
-  AllocMapType::iterator ItEnd = m_AllocatedBufferMap.end();<br>
-  for (AllocMapType::iterator It = ItBegin; It != ItEnd; ++It) {<br>
-    uint64_t A = It->first;<br>
-    uint64_t E = A + It->second;<br>
-    // Starts and finishes inside allocated region<br>
-    if (Address >= A && End <= E)<br>
-      return true;<br>
-  }<br>
-  return false;<br>
-}<br>
-<br>
 // Outgoing message handlers<br>
 void LLIChildTarget::sendChildActive() {<br>
   // Write the message type.<br>
<br>
Modified: llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc (original)<br>
+++ llvm/trunk/tools/lli/ChildTarget/Unix/ChildTarget.inc Thu Jan 23 05:04:42 2014<br>
@@ -16,28 +16,6 @@<br>
 #include <stdlib.h><br>
 #include <unistd.h><br>
<br>
-#ifdef HAVE_SYS_MMAN_H<br>
-#include <sys/mman.h><br>
-#endif<br>
-<br>
-#ifdef __APPLE__<br>
-#include <mach/mach.h><br>
-#endif<br>
-<br>
-#if defined(__mips__)<br>
-#  if defined(__OpenBSD__)<br>
-#    include <mips64/sysarch.h><br>
-#  else<br>
-#    include <sys/cachectl.h><br>
-#  endif<br>
-#endif<br>
-<br>
-#ifdef __APPLE__<br>
-extern "C" void sys_icache_invalidate(const void *Addr, size_t len);<br>
-#else<br>
-extern "C" void __clear_cache(void *, void*);<br>
-#endif<br>
-<br>
 namespace {<br>
<br>
 struct ConnectionData_t {<br>
@@ -66,101 +44,3 @@ int LLIChildTarget::WriteBytes(const voi<br>
 int LLIChildTarget::ReadBytes(void *Data, size_t Size) {<br>
   return read(((ConnectionData_t*)ConnectionData)->InputPipe, Data, Size);<br>
 }<br>
-<br>
-// The functions below duplicate functionality that is implemented in<br>
-// Support/Memory.cpp with the goal of avoiding a dependency on any<br>
-// llvm libraries.<br>
-<br>
-uint64_t LLIChildTarget::allocate(uint32_t Alignment, uint32_t Size) {<br>
-  if (!Alignment)<br>
-    Alignment = 16;<br>
-<br>
-  static const size_t PageSize = getpagesize();<br>
-  const size_t NumPages = (Size+PageSize-1)/PageSize;<br>
-  Size = NumPages*PageSize;<br>
-<br>
-  int fd = -1;<br>
-#ifdef NEED_DEV_ZERO_FOR_MMAP<br>
-  static int zero_fd = open("/dev/zero", O_RDWR);<br>
-  if (zero_fd == -1)<br>
-    return 0;<br>
-  fd = zero_fd;<br>
-#endif<br>
-<br>
-  int MMFlags = MAP_PRIVATE |<br>
-#ifdef HAVE_MMAP_ANONYMOUS<br>
-  MAP_ANONYMOUS<br>
-#else<br>
-  MAP_ANON<br>
-#endif<br>
-  ; // Ends statement above<br>
-<br>
-  uint64_t Addr = (uint64_t)::mmap(0, Size, PROT_READ | PROT_WRITE, MMFlags, fd, 0);<br>
-  if (Addr == (uint64_t)MAP_FAILED)<br>
-    return 0;<br>
-<br>
-  // Align the address.<br>
-  Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);<br>
-<br>
-  m_AllocatedBufferMap[Addr] = Size;<br>
-<br>
-  // Return aligned address<br>
-  return Addr;<br>
-}<br>
-<br>
-void LLIChildTarget::makeSectionExecutable(uint64_t Addr, uint32_t Size) {<br>
-  // FIXME: We have to mark the memory as RWX because multiple code chunks may<br>
-  // be on the same page.  The RemoteTarget interface should be changed to<br>
-  // work around that.<br>
-  int Result = ::mprotect((void*)Addr, Size, PROT_READ | PROT_WRITE | PROT_EXEC);<br>
-  if (Result != 0)<br>
-    InvalidateInstructionCache((const void *)Addr, Size);<br>
-}<br>
-<br>
-/// InvalidateInstructionCache - Before the JIT can run a block of code<br>
-/// that has been emitted it must invalidate the instruction cache on some<br>
-/// platforms.<br>
-void LLIChildTarget::InvalidateInstructionCache(const void *Addr,<br>
-                                        size_t Len) {<br>
-<br>
-// icache invalidation for PPC and ARM.<br>
-#if defined(__APPLE__)<br>
-<br>
-#  if (defined(__POWERPC__) || defined (__ppc__) || \<br>
-     defined(_POWER) || defined(_ARCH_PPC)) || defined(__arm__)<br>
-  sys_icache_invalidate(const_cast<void *>(Addr), Len);<br>
-#  endif<br>
-<br>
-#else<br>
-<br>
-#  if (defined(__POWERPC__) || defined (__ppc__) || \<br>
-       defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)<br>
-  const size_t LineSize = 32;<br>
-<br>
-  const intptr_t Mask = ~(LineSize - 1);<br>
-  const intptr_t StartLine = ((intptr_t) Addr) & Mask;<br>
-  const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;<br>
-<br>
-  for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)<br>
-    asm volatile("dcbf 0, %0" : : "r"(Line));<br>
-  asm volatile("sync");<br>
-<br>
-  for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)<br>
-    asm volatile("icbi 0, %0" : : "r"(Line));<br>
-  asm volatile("isync");<br>
-#  elif defined(__arm__) && defined(__GNUC__)<br>
-  // FIXME: Can we safely always call this for __GNUC__ everywhere?<br>
-  const char *Start = static_cast<const char *>(Addr);<br>
-  const char *End = Start + Len;<br>
-  __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));<br>
-#  elif defined(__mips__)<br>
-  const char *Start = static_cast<const char *>(Addr);<br>
-  cacheflush(const_cast<char *>(Start), Len, BCACHE);<br>
-#  endif<br>
-<br>
-#endif  // end apple<br>
-}<br>
-<br>
-void LLIChildTarget::releaseMemory(uint64_t Addr, uint32_t Size) {<br>
-  ::munmap((void*)Addr, Size);<br>
-}<br>
<br>
Modified: llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc (original)<br>
+++ llvm/trunk/tools/lli/ChildTarget/Windows/ChildTarget.inc Thu Jan 23 05:04:42 2014<br>
@@ -32,13 +32,3 @@ int LLIChildTarget::ReadBytes(void *Data<br>
 uint64_t LLIChildTarget::allocate(uint32_t Alignment, uint32_t Size) {<br>
   return 0;<br>
 }<br>
-<br>
-void LLIChildTarget::makeSectionExecutable(uint64_t Addr, uint32_t Size) {<br>
-}<br>
-<br>
-void LLIChildTarget::InvalidateInstructionCache(const void *Addr,<br>
-                                        size_t Len) {<br>
-}<br>
-<br>
-void LLIChildTarget::releaseMemory(uint64_t Addr, uint32_t Size) {<br>
-}<br>
<br>
Modified: llvm/trunk/tools/lli/RemoteTarget.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/RemoteTarget.cpp?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/RemoteTarget.cpp?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/RemoteTarget.cpp (original)<br>
+++ llvm/trunk/tools/lli/RemoteTarget.cpp Thu Jan 23 05:04:42 2014<br>
@@ -62,6 +62,7 @@ bool RemoteTarget::allocateSpace(size_t<br>
     return false;<br>
   }<br>
   Address = reinterpret_cast<uint64_t>(Mem.base());<br>
+  Allocations.push_back(Mem);<br>
   return true;<br>
 }<br>
<br>
<br>
Modified: llvm/trunk/tools/lli/RemoteTarget.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/RemoteTarget.h?rev=199881&r1=199880&r2=199881&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lli/RemoteTarget.h?rev=199881&r1=199880&r2=199881&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/lli/RemoteTarget.h (original)<br>
+++ llvm/trunk/tools/lli/RemoteTarget.h Thu Jan 23 05:04:42 2014<br>
@@ -27,7 +27,8 @@ namespace llvm {<br>
 class RemoteTarget {<br>
   bool IsRunning;<br>
<br>
-  SmallVector<sys::MemoryBlock, 16> Allocations;<br>
+  typedef SmallVector<sys::MemoryBlock, 16> AllocMapType;<br>
+  AllocMapType Allocations;<br>
<br>
 protected:<br>
   std::string ErrorMsg;<br>
@@ -47,6 +48,18 @@ public:<br>
                              unsigned Alignment,<br>
                              uint64_t &Address);<br>
<br>
+  bool isAllocatedMemory(uint64_t Address, uint32_t Size) {<br>
+    uint64_t AddressEnd = Address + Size;<br>
+    for (AllocMapType::const_iterator I = Allocations.begin(),<br>
+                                      E = Allocations.end();<br>
+         I != E; ++I) {<br>
+      if (Address >= (uint64_t)I->base() &&<br>
+          AddressEnd <= (uint64_t)I->base() + I->size())<br>
+        return true;<br>
+    }<br>
+    return false;<br>
+  }<br>
+<br>
   /// Load data into the target address space.<br>
   ///<br>
   /// @param      Address   Destination address in the target process.<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>