Abstract the Routine class.

Bug swiftshader:10

Change-Id: I29b1de8c1adb67449a380c307d12e2aea21f32cc
Reviewed-on: https://swiftshader-review.googlesource.com/7251
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/.gitignore b/.gitignore
index 093d1ed..86187e9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,25 +1,28 @@
-# Ignored folders #

-/lib/

-/obj/

-/bin/

-

-# Ignored files #

-*.obj

-*.lib

-*.log

-*.tlog

-*.exe

-*.ilk

-*.pdb

-*.sbr

-*.bsc

-*.dll

-*.res

-*.idb

-*.sdf

-*.suo

-*.o

-*.depend

-*.layout

-*.opensdf

-*.aps

+# Ignored folders #
+/lib/
+/obj/
+/bin/
+.vs
+
+# Ignored files #
+*.obj
+*.lib
+*.log
+*.tlog
+*.exe
+*.ilk
+*.pdb
+*.sbr
+*.bsc
+*.dll
+*.res
+*.idb
+*.sdf
+*.suo
+*.o
+*.depend
+*.layout
+*.opensdf
+*.aps
+*.opendb
+*.db
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c147ac9..5000bc3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -605,8 +605,10 @@
     ${SOURCE_DIR}/Reactor/Nucleus.hpp
     ${SOURCE_DIR}/Reactor/Routine.cpp
     ${SOURCE_DIR}/Reactor/Routine.hpp
-    ${SOURCE_DIR}/Reactor/RoutineManager.cpp
-    ${SOURCE_DIR}/Reactor/RoutineManager.hpp
+    ${SOURCE_DIR}/Reactor/LLVMRoutine.cpp
+    ${SOURCE_DIR}/Reactor/LLVMRoutine.hpp
+    ${SOURCE_DIR}/Reactor/LLVMRoutineManager.cpp
+    ${SOURCE_DIR}/Reactor/LLVMRoutineManager.hpp
 )
 
 file(GLOB_RECURSE EGL_LIST
diff --git a/src/Android.mk b/src/Android.mk
index d3efe38..9fffda3 100644
--- a/src/Android.mk
+++ b/src/Android.mk
@@ -38,7 +38,8 @@
 COMMON_SRC_FILES += \
 	Reactor/Nucleus.cpp \
 	Reactor/Routine.cpp \
-	Reactor/RoutineManager.cpp
+	Reactor/LLVMRoutine.cpp \
+	Reactor/LLVMRoutineManager.cpp
 
 COMMON_SRC_FILES += \
 	Renderer/Blitter.cpp \
diff --git a/src/OpenGL/libGLES_CM/libGLES_CM.cbp b/src/OpenGL/libGLES_CM/libGLES_CM.cbp
index e5028ba..1123a3e 100644
--- a/src/OpenGL/libGLES_CM/libGLES_CM.cbp
+++ b/src/OpenGL/libGLES_CM/libGLES_CM.cbp
@@ -178,8 +178,10 @@
 		<Unit filename="../../Reactor/Reactor.hpp" />
 		<Unit filename="../../Reactor/Routine.cpp" />
 		<Unit filename="../../Reactor/Routine.hpp" />
-		<Unit filename="../../Reactor/RoutineManager.cpp" />
-		<Unit filename="../../Reactor/RoutineManager.hpp" />
+		<Unit filename="../../Reactor/LLVMRoutine.cpp" />
+		<Unit filename="../../Reactor/LLVMRoutine.hpp" />
+		<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
+		<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
 		<Unit filename="../../Reactor/x86.hpp" />
 		<Unit filename="../../Renderer/Blitter.cpp" />
 		<Unit filename="../../Renderer/Blitter.hpp" />
diff --git a/src/OpenGL/libGLESv2/libGLESv2.cbp b/src/OpenGL/libGLESv2/libGLESv2.cbp
index abd8fe8..7cc601f 100644
--- a/src/OpenGL/libGLESv2/libGLESv2.cbp
+++ b/src/OpenGL/libGLESv2/libGLESv2.cbp
@@ -177,8 +177,10 @@
 		<Unit filename="../../Reactor/Reactor.hpp" />
 		<Unit filename="../../Reactor/Routine.cpp" />
 		<Unit filename="../../Reactor/Routine.hpp" />
-		<Unit filename="../../Reactor/RoutineManager.cpp" />
-		<Unit filename="../../Reactor/RoutineManager.hpp" />
+		<Unit filename="../../Reactor/LLVMRoutine.cpp" />
+		<Unit filename="../../Reactor/LLVMRoutine.hpp" />
+		<Unit filename="../../Reactor/LLVMRoutineManager.cpp" />
+		<Unit filename="../../Reactor/LLVMRoutineManager.hpp" />
 		<Unit filename="../../Reactor/x86.hpp" />
 		<Unit filename="../../Renderer/Blitter.cpp" />
 		<Unit filename="../../Renderer/Blitter.hpp" />
diff --git a/src/Reactor/BUILD.gn b/src/Reactor/BUILD.gn
index 0e3e8f4..ee04cbf 100644
--- a/src/Reactor/BUILD.gn
+++ b/src/Reactor/BUILD.gn
@@ -43,7 +43,8 @@
   sources = [
     "Nucleus.cpp",
     "Routine.cpp",
-    "RoutineManager.cpp",
+    "LLVMRoutine.cpp",
+    "LLVMRoutineManager.cpp",
   ]
 
   if (is_win) {
diff --git a/src/Reactor/LLVMRoutine.cpp b/src/Reactor/LLVMRoutine.cpp
new file mode 100644
index 0000000..df12337
--- /dev/null
+++ b/src/Reactor/LLVMRoutine.cpp
@@ -0,0 +1,46 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "LLVMRoutine.hpp"
+
+#include "../Common/Memory.hpp"
+#include "../Common/Thread.hpp"
+#include "../Common/Types.hpp"
+
+namespace sw
+{
+	LLVMRoutine::LLVMRoutine(int bufferSize) : bufferSize(bufferSize)
+	{
+		void *memory = allocateExecutable(bufferSize);
+
+		buffer = memory;
+		entry = memory;
+		functionSize = bufferSize;   // Updated by LLVMRoutineManager::endFunctionBody
+	}
+
+	LLVMRoutine::~LLVMRoutine()
+	{
+		deallocateExecutable(buffer, bufferSize);
+	}
+
+	const void *LLVMRoutine::getEntry()
+	{
+		return entry;
+	}
+
+	int LLVMRoutine::getCodeSize()
+	{
+		return functionSize - static_cast<int>((uintptr_t)entry - (uintptr_t)buffer);
+	}
+}
diff --git a/src/Reactor/LLVMRoutine.hpp b/src/Reactor/LLVMRoutine.hpp
new file mode 100644
index 0000000..264990f
--- /dev/null
+++ b/src/Reactor/LLVMRoutine.hpp
@@ -0,0 +1,53 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef sw_LLVMRoutine_hpp
+#define sw_LLVMRoutine_hpp
+
+#include "Routine.hpp"
+
+namespace sw
+{
+	class LLVMRoutineManager;
+
+	class LLVMRoutine : public Routine
+	{
+		friend class LLVMRoutineManager;
+
+	public:
+		LLVMRoutine(int bufferSize);
+		//LLVMRoutine(void *memory, int bufferSize, int offset);
+
+		virtual ~LLVMRoutine();
+
+		//void setFunctionSize(int functionSize);
+
+		//const void *getBuffer();
+		const void *getEntry();
+		//int getBufferSize();
+		//int getFunctionSize();   // Includes constants before the entry point
+		int getCodeSize();       // Executable code only
+		//bool isDynamic();
+
+	private:
+		void *buffer;
+		const void *entry;
+		int bufferSize;
+		int functionSize;
+
+		//const bool dynamic;   // Generated or precompiled
+	};
+}
+
+#endif   // sw_LLVMRoutine_hpp
diff --git a/src/Reactor/LLVMRoutineManager.cpp b/src/Reactor/LLVMRoutineManager.cpp
new file mode 100644
index 0000000..99a8e9d
--- /dev/null
+++ b/src/Reactor/LLVMRoutineManager.cpp
@@ -0,0 +1,148 @@
+// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "LLVMRoutineManager.hpp"
+
+#include "LLVMRoutine.hpp"
+#include "llvm/Function.h"
+#include "../Common/Memory.hpp"
+#include "../Common/Thread.hpp"
+#include "../Common/Debug.hpp"
+
+namespace sw
+{
+	using namespace llvm;
+
+	volatile int LLVMRoutineManager::averageInstructionSize = 4;
+
+	LLVMRoutineManager::LLVMRoutineManager()
+	{
+		routine = nullptr;
+	}
+
+	LLVMRoutineManager::~LLVMRoutineManager()
+	{
+		delete routine;
+	}
+
+	void LLVMRoutineManager::AllocateGOT()
+	{
+		UNIMPLEMENTED();
+	}
+
+	uint8_t *LLVMRoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment)
+	{
+		UNIMPLEMENTED();
+		return nullptr;
+	}
+
+	uint8_t *LLVMRoutineManager::startFunctionBody(const llvm::Function *function, uintptr_t &actualSize)
+	{
+		if(actualSize == 0)   // Estimate size
+		{
+			size_t instructionCount = 0;
+			for(llvm::Function::const_iterator basicBlock = function->begin(); basicBlock != function->end(); basicBlock++)
+			{
+				instructionCount += basicBlock->size();
+			}
+
+			actualSize = instructionCount * averageInstructionSize;
+		}
+		else   // Estimate was too low
+		{
+			sw::atomicIncrement(&averageInstructionSize);
+		}
+
+		// Round up to the next page size
+		size_t pageSize = memoryPageSize();
+		actualSize = (actualSize + pageSize - 1) & ~(pageSize - 1);
+
+		delete routine;
+		routine = new LLVMRoutine(static_cast<int>(actualSize));
+
+		return (uint8_t*)routine->buffer;
+	}
+
+	void LLVMRoutineManager::endFunctionBody(const llvm::Function *function, uint8_t *functionStart, uint8_t *functionEnd)
+	{
+		routine->functionSize = static_cast<int>(static_cast<ptrdiff_t>(functionEnd - functionStart));
+	}
+
+	uint8_t *LLVMRoutineManager::startExceptionTable(const llvm::Function* F, uintptr_t &ActualSize)
+	{
+		UNIMPLEMENTED();
+		return nullptr;
+	}
+
+	void LLVMRoutineManager::endExceptionTable(const llvm::Function *F, uint8_t *TableStart, uint8_t *TableEnd, uint8_t* FrameRegister)
+	{
+		UNIMPLEMENTED();
+	}
+
+	uint8_t *LLVMRoutineManager::getGOTBase() const
+	{
+		ASSERT(!HasGOT);
+		return nullptr;
+	}
+
+	uint8_t *LLVMRoutineManager::allocateSpace(intptr_t Size, unsigned Alignment)
+	{
+		UNIMPLEMENTED();
+		return nullptr;
+	}
+
+	uint8_t *LLVMRoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
+	{
+		UNIMPLEMENTED();
+		return nullptr;
+	}
+
+	void LLVMRoutineManager::deallocateFunctionBody(void *Body)
+	{
+		delete routine;
+		routine = nullptr;
+	}
+
+	void LLVMRoutineManager::deallocateExceptionTable(void *ET)
+	{
+		if(ET)
+		{
+			UNIMPLEMENTED();
+		}
+	}
+
+	void LLVMRoutineManager::setMemoryWritable()
+	{
+	}
+
+	void LLVMRoutineManager::setMemoryExecutable()
+	{
+		markExecutable(routine->buffer, routine->bufferSize);
+	}
+
+	void LLVMRoutineManager::setPoisonMemory(bool poison)
+	{
+		UNIMPLEMENTED();
+	}
+
+	LLVMRoutine *LLVMRoutineManager::acquireRoutine(void *entry)
+	{
+		routine->entry = entry;
+
+		LLVMRoutine *result = routine;
+		routine = nullptr;
+
+		return result;
+	}
+}
diff --git a/src/Reactor/RoutineManager.hpp b/src/Reactor/LLVMRoutineManager.hpp
similarity index 85%
rename from src/Reactor/RoutineManager.hpp
rename to src/Reactor/LLVMRoutineManager.hpp
index fb85fe4..b235923 100644
--- a/src/Reactor/RoutineManager.hpp
+++ b/src/Reactor/LLVMRoutineManager.hpp
@@ -12,22 +12,22 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#ifndef sw_RoutineManager_hpp
-#define sw_RoutineManager_hpp
+#ifndef sw_LLVMRoutineManager_hpp
+#define sw_LLVMRoutineManager_hpp
 
 #include "llvm/GlobalValue.h"
 #include "llvm/ExecutionEngine/JITMemoryManager.h"
 
 namespace sw
 {
-	class Routine;
+	class LLVMRoutine;
 
-	class RoutineManager : public llvm::JITMemoryManager
+	class LLVMRoutineManager : public llvm::JITMemoryManager
 	{
 	public:
-		RoutineManager();
+		LLVMRoutineManager();
 
-		virtual ~RoutineManager();
+		virtual ~LLVMRoutineManager();
 
 		virtual void AllocateGOT();
 
@@ -45,13 +45,13 @@
 		virtual void setMemoryExecutable();
 		virtual void setPoisonMemory(bool poison);
 
-		Routine *acquireRoutine(void *entry);
+		LLVMRoutine *acquireRoutine(void *entry);
 
 	private:
-		Routine *routine;
+		LLVMRoutine *routine;
 
 		static volatile int averageInstructionSize;
 	};
 }
 
-#endif   // sw_RoutineManager_hpp
+#endif   // sw_LLVMRoutineManager_hpp
diff --git a/src/Reactor/Nucleus.cpp b/src/Reactor/Nucleus.cpp
index 2b5baaf..67dd392 100644
--- a/src/Reactor/Nucleus.cpp
+++ b/src/Reactor/Nucleus.cpp
@@ -29,8 +29,8 @@
 #include "llvm/Support/TargetSelect.h"
 #include "../lib/ExecutionEngine/JIT/JIT.h"
 
-#include "Routine.hpp"
-#include "RoutineManager.hpp"
+#include "LLVMRoutine.hpp"
+#include "LLVMRoutineManager.hpp"
 #include "x86.hpp"
 #include "CPUID.hpp"
 #include "Thread.hpp"
@@ -60,7 +60,7 @@
 
 namespace
 {
-	sw::RoutineManager *routineManager = nullptr;
+	sw::LLVMRoutineManager *routineManager = nullptr;
 	llvm::ExecutionEngine *executionEngine = nullptr;
 	llvm::IRBuilder<> *builder = nullptr;
 	llvm::LLVMContext *context = nullptr;
@@ -118,7 +118,7 @@
 		}
 
 		::module = new Module("", *::context);
-		::routineManager = new RoutineManager();
+		::routineManager = new LLVMRoutineManager();
 
 		#if defined(__x86_64__)
 			const char *architecture = "x86-64";
@@ -205,7 +205,7 @@
 		}
 
 		void *entry = ::executionEngine->getPointerToFunction(::function);
-		Routine *routine = ::routineManager->acquireRoutine(entry);
+		LLVMRoutine *routine = ::routineManager->acquireRoutine(entry);
 
 		if(CodeAnalystLogJITCode)
 		{
diff --git a/src/Reactor/Nucleus.hpp b/src/Reactor/Nucleus.hpp
index 0c53ca0..27f833f 100644
--- a/src/Reactor/Nucleus.hpp
+++ b/src/Reactor/Nucleus.hpp
@@ -34,6 +34,7 @@
 	class Value;
 	class Constant;
 	class BasicBlock;
+	class Routine;
 
 	enum Optimization
 	{
@@ -53,10 +54,6 @@
 
 	extern Optimization optimization[10];
 
-	class Routine;
-	class RoutineManager;
-	class Builder;
-
 	class Nucleus
 	{
 	public:
diff --git a/src/Reactor/Reactor.vcxproj b/src/Reactor/Reactor.vcxproj
index f9e24b6..0e15e93 100644
--- a/src/Reactor/Reactor.vcxproj
+++ b/src/Reactor/Reactor.vcxproj
@@ -266,15 +266,17 @@
     </ProjectReference>

   </ItemDefinitionGroup>

   <ItemGroup>

+    <ClCompile Include="LLVMRoutine.cpp" />

+    <ClCompile Include="LLVMRoutineManager.cpp" />

     <ClCompile Include="Nucleus.cpp" />

     <ClCompile Include="Routine.cpp" />

-    <ClCompile Include="RoutineManager.cpp" />

   </ItemGroup>

   <ItemGroup>

+    <ClInclude Include="LLVMRoutine.hpp" />

+    <ClInclude Include="LLVMRoutineManager.hpp" />

     <ClInclude Include="Nucleus.hpp" />

     <ClInclude Include="Reactor.hpp" />

     <ClInclude Include="Routine.hpp" />

-    <ClInclude Include="RoutineManager.hpp" />

     <ClInclude Include="x86.hpp" />

   </ItemGroup>

   <ItemGroup>

diff --git a/src/Reactor/Reactor.vcxproj.filters b/src/Reactor/Reactor.vcxproj.filters
index 2a86f26..27a7fde 100644
--- a/src/Reactor/Reactor.vcxproj.filters
+++ b/src/Reactor/Reactor.vcxproj.filters
@@ -18,10 +18,13 @@
     <ClCompile Include="Nucleus.cpp">

       <Filter>Source Files</Filter>

     </ClCompile>

-    <ClCompile Include="RoutineManager.cpp">

+    <ClCompile Include="Routine.cpp">

       <Filter>Source Files</Filter>

     </ClCompile>

-    <ClCompile Include="Routine.cpp">

+    <ClCompile Include="LLVMRoutineManager.cpp">

+      <Filter>Source Files</Filter>

+    </ClCompile>

+    <ClCompile Include="LLVMRoutine.cpp">

       <Filter>Source Files</Filter>

     </ClCompile>

   </ItemGroup>

@@ -35,10 +38,13 @@
     <ClInclude Include="x86.hpp">

       <Filter>Header Files</Filter>

     </ClInclude>

-    <ClInclude Include="RoutineManager.hpp">

+    <ClInclude Include="Routine.hpp">

       <Filter>Header Files</Filter>

     </ClInclude>

-    <ClInclude Include="Routine.hpp">

+    <ClInclude Include="LLVMRoutineManager.hpp">

+      <Filter>Header Files</Filter>

+    </ClInclude>

+    <ClInclude Include="LLVMRoutine.hpp">

       <Filter>Header Files</Filter>

     </ClInclude>

   </ItemGroup>

diff --git a/src/Reactor/Routine.cpp b/src/Reactor/Routine.cpp
index 340b21d..df82ab8 100644
--- a/src/Reactor/Routine.cpp
+++ b/src/Reactor/Routine.cpp
@@ -14,74 +14,17 @@
 
 #include "Routine.hpp"
 
-#include "../Common/Memory.hpp"
 #include "../Common/Thread.hpp"
-#include "../Common/Types.hpp"
+
+#include <cassert>
 
 namespace sw
 {
-	Routine::Routine(int bufferSize) : bufferSize(bufferSize), dynamic(true)
+	Routine::Routine()
 	{
-		void *memory = allocateExecutable(bufferSize);
-
-		buffer = memory;
-		entry = memory;
-		functionSize = bufferSize;   // Updated by RoutineManager::endFunctionBody
-
 		bindCount = 0;
 	}
 
-	Routine::Routine(void *memory, int bufferSize, int offset) : bufferSize(bufferSize), functionSize(bufferSize), dynamic(false)
-	{
-		buffer = (unsigned char*)memory - offset;
-		entry = memory;
-
-		bindCount = 0;
-	}
-
-	Routine::~Routine()
-	{
-		if(dynamic)
-		{
-			deallocateExecutable(buffer, bufferSize);
-		}
-	}
-
-	void Routine::setFunctionSize(int functionSize)
-	{
-		this->functionSize = functionSize;
-	}
-
-	const void *Routine::getBuffer()
-	{
-		return buffer;
-	}
-
-	const void *Routine::getEntry()
-	{
-		return entry;
-	}
-
-	int Routine::getBufferSize()
-	{
-		return bufferSize;
-	}
-
-	int Routine::getFunctionSize()
-	{
-		return functionSize;
-	}
-
-	int Routine::getCodeSize()
-	{
-		return functionSize - static_cast<int>((uintptr_t)entry - (uintptr_t)buffer);
-	}
-
-	bool Routine::isDynamic()
-	{
-		return dynamic;
-	}
-
 	void Routine::bind()
 	{
 		atomicIncrement(&bindCount);
@@ -96,4 +39,9 @@
 			delete this;
 		}
 	}
+
+	Routine::~Routine()
+	{
+		assert(bindCount == 0);
+	}
 }
diff --git a/src/Reactor/Routine.hpp b/src/Reactor/Routine.hpp
index 6252a7a..b3ad7f5 100644
--- a/src/Reactor/Routine.hpp
+++ b/src/Reactor/Routine.hpp
@@ -17,38 +17,21 @@
 
 namespace sw
 {
-	class RoutineManager;
-
 	class Routine
 	{
-		friend class RoutineManager;
-
 	public:
-		Routine(int bufferSize);
-		Routine(void *memory, int bufferSize, int offset);
+		Routine();
 
-		~Routine();
+		virtual ~Routine();
 
-		void setFunctionSize(int functionSize);
+		virtual const void *getEntry() = 0;
 
-		const void *getBuffer();
-		const void *getEntry();
-		int getBufferSize();
-		int getFunctionSize();   // Includes constants before the entry point
-		int getCodeSize();       // Executable code only
-		bool isDynamic();
-
+		// Reference counting
 		void bind();
 		void unbind();
 
 	private:
-		void *buffer;
-		const void *entry;
-		int bufferSize;
-		int functionSize;
-
 		volatile int bindCount;
-		const bool dynamic;   // Generated or precompiled
 	};
 }
 
diff --git a/src/Reactor/RoutineManager.cpp b/src/Reactor/RoutineManager.cpp
deleted file mode 100644
index 928bec4..0000000
--- a/src/Reactor/RoutineManager.cpp
+++ /dev/null
@@ -1,148 +0,0 @@
-// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "RoutineManager.hpp"
-
-#include "Routine.hpp"
-#include "llvm/Function.h"
-#include "../Common/Memory.hpp"
-#include "../Common/Thread.hpp"
-#include "../Common/Debug.hpp"
-
-namespace sw
-{
-	using namespace llvm;
-
-	volatile int RoutineManager::averageInstructionSize = 4;
-
-	RoutineManager::RoutineManager()
-	{
-		routine = 0;
-	}
-
-	RoutineManager::~RoutineManager()
-	{
-		delete routine;
-	}
-
-	void RoutineManager::AllocateGOT()
-	{
-		UNIMPLEMENTED();
-	}
-
-	uint8_t *RoutineManager::allocateStub(const GlobalValue *function, unsigned stubSize, unsigned alignment)
-	{
-		UNIMPLEMENTED();
-		return 0;
-	}
-
-	uint8_t *RoutineManager::startFunctionBody(const llvm::Function *function, uintptr_t &actualSize)
-	{
-		if(actualSize == 0)   // Estimate size
-		{
-			size_t instructionCount = 0;
-			for(llvm::Function::const_iterator basicBlock = function->begin(); basicBlock != function->end(); basicBlock++)
-			{
-				instructionCount += basicBlock->size();
-			}
-
-			actualSize = instructionCount * averageInstructionSize;
-		}
-		else   // Estimate was too low
-		{
-			sw::atomicIncrement(&averageInstructionSize);
-		}
-
-		// Round up to the next page size
-		size_t pageSize = memoryPageSize();
-		actualSize = (actualSize + pageSize - 1) & ~(pageSize - 1);
-
-		delete routine;
-		routine = new Routine(static_cast<int>(actualSize));
-
-		return (uint8_t*)routine->getBuffer();
-	}
-
-	void RoutineManager::endFunctionBody(const llvm::Function *function, uint8_t *functionStart, uint8_t *functionEnd)
-	{
-		routine->setFunctionSize(static_cast<int>(static_cast<ptrdiff_t>(functionEnd - functionStart)));
-	}
-
-	uint8_t *RoutineManager::startExceptionTable(const llvm::Function* F, uintptr_t &ActualSize)
-	{
-		UNIMPLEMENTED();
-		return 0;
-	}
-
-	void RoutineManager::endExceptionTable(const llvm::Function *F, uint8_t *TableStart, uint8_t *TableEnd, uint8_t* FrameRegister)
-	{
-		UNIMPLEMENTED();
-	}
-
-	uint8_t *RoutineManager::getGOTBase() const
-	{
-		ASSERT(!HasGOT);
-		return 0;
-	}
-
-	uint8_t *RoutineManager::allocateSpace(intptr_t Size, unsigned Alignment)
-	{
-		UNIMPLEMENTED();
-		return 0;
-	}
-
-	uint8_t *RoutineManager::allocateGlobal(uintptr_t Size, unsigned Alignment)
-	{
-		UNIMPLEMENTED();
-		return 0;
-	}
-
-	void RoutineManager::deallocateFunctionBody(void *Body)
-	{
-		delete routine;
-		routine = 0;
-	}
-
-	void RoutineManager::deallocateExceptionTable(void *ET)
-	{
-		if(ET)
-		{
-			UNIMPLEMENTED();
-		}
-	}
-
-	void RoutineManager::setMemoryWritable()
-	{
-	}
-
-	void RoutineManager::setMemoryExecutable()
-	{
-		markExecutable(routine->buffer, routine->bufferSize);
-	}
-
-	void RoutineManager::setPoisonMemory(bool poison)
-	{
-		UNIMPLEMENTED();
-	}
-
-	Routine *RoutineManager::acquireRoutine(void *entry)
-	{
-		routine->entry = entry;
-
-		Routine *result = routine;
-		routine = 0;
-
-		return result;
-	}
-}