##############################################################
set (GBE_DEBUG_MEMORY false CACHE bool "Activate the memory debugger")
+set (GBE_COMPILE_UTEST false CACHE bool "Will compile the unit tests")
set (GBE_USE_BLOB false CACHE bool "Compile everything from one big file")
-set (GBE_VERBOSE_VECTORIZER false CACHE bool "Output vectorizer diagnostic (GCC only)")
##############################################################
# Compiler
set (GBE_DEBUG_MEMORY_FLAG "${DEF}GBE_DEBUG_MEMORY=0")
endif (GBE_DEBUG_MEMORY)
+if (GBE_COMPILE_UTEST)
+ set (GBE_COMPILE_UTESTS_FLAG "${DEF}GBE_COMPILE_UTESTS=1")
+else (GBE_COMPILE_UTEST)
+ set (GBE_COMPILE_UTESTS_FLAG "${DEF}GBE_COMPILE_UTESTS=0")
+endif (GBE_COMPILE_UTEST)
+
## Linux compilation
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Hide all symbols and allows the symbols declared as visible to be exported
if (COMPILER STREQUAL "GCC")
set (CMAKE_CXX_FLAGS "-Wstrict-aliasing=2 -Wno-invalid-offsetof -fno-strict-aliasing -msse2 -ffast-math -fPIC -Wall -fno-rtti -std=c++0x")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG}")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_COMPILE_UTESTS_FLAG}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VISIBILITY_FLAG} -Wl,-E")
+ if (NOT GBE_COMPILE_UTEST)
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
+ endif (NOT GBE_COMPILE_UTEST)
set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG -ftree-vectorize")
- if (GBE_VERBOSE_VECTORIZER)
- set (CMAKE_CXX_FLAGS "-ftree-vectorizer-verbose=2")
- endif (GBE_VERBOSE_VECTORIZER)
elseif (COMPILER STREQUAL "CLANG")
set (CMAKE_C_COMPILER "clang")
set (CMAKE_C_FLAGS "-Wall -std=c99")
set (CMAKE_CXX_COMPILER "clang++")
set (CMAKE_CXX_FLAGS "-fno-strict-aliasing -msse2 -ffast-math -fPIC -Wall -Wno-format-security -Wno-invalid-offsetof -std=c++0x")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG}")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_COMPILE_UTESTS_FLAG}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VISIBILITY_FLAG}")
set (CMAKE_CXX_FLAGS_DEBUG "-g")
set (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set (CMAKE_C_COMPILER "icc")
set (CMAKE_CXX_FLAGS "-std=c++0x -wd2928 -Wall -fPIC -fno-strict-aliasing -fp-model fast -xSSE2")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG}")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_COMPILE_UTESTS_FLAG}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${VISIBILITY_FLAG} -Wl,-E")
set (CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set (CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2")
## Windows compilation
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
if (MINGW)
- if (GBE_VERBOSE_VECTORIZER)
- set (CMAKE_CXX_FLAGS "-ftree-vectorizer-verbose=2")
- endif (GBE_VERBOSE_VECTORIZER)
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG} -Wno-invalid-offsetof -fstrict-aliasing -msse2 -ffast-math -Wall -fno-rtti -std=c++0x")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -fstrict-aliasing -msse2 -ffast-math -Wall -fno-rtti -std=c++0x")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_DEBUG_MEMORY_FLAG}")
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GBE_COMPILE_UTESTS_FLAG}")
set (CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
else (MINGW)
set (COMMON_FLAGS "${GBE_DEBUG_MEMORY_FLAG} /arch:SSE2 /D_CRT_SECURE_NO_WARNINGS /DNOMINMAX /GR- /GS- /W3 /wd4275")
-set (GBE_SRC
- sys/vector.hpp
- sys/hash_map.hpp
- sys/map.hpp
- sys/set.hpp
- sys/exception.hpp
- sys/assert.cpp
- sys/assert.hpp
- sys/string.cpp
- sys/string.hpp
- sys/alloc.cpp
- sys/alloc.hpp
- sys/sysinfo.cpp
- sys/sysinfo.hpp
- sys/mutex.cpp
- sys/mutex.hpp
- sys/condition.cpp
- sys/condition.hpp
- sys/platform.cpp
- sys/platform.hpp
- ir/context.cpp
- ir/context.hpp
- ir/unit.cpp
- ir/unit.hpp
- ir/constant.cpp
- ir/constant.hpp
- ir/instruction.cpp
- ir/instruction.hpp
- ir/register.cpp
- ir/register.hpp
- ir/function.cpp
- ir/function.hpp)
+if (GBE_USE_BLOB)
+ set (GBE_SRC blob.cpp)
+else (GBE_USE_BLOB)
+ set (GBE_SRC
+ sys/vector.hpp
+ sys/hash_map.hpp
+ sys/map.hpp
+ sys/set.hpp
+ sys/exception.hpp
+ sys/assert.cpp
+ sys/assert.hpp
+ sys/string.cpp
+ sys/string.hpp
+ sys/alloc.cpp
+ sys/alloc.hpp
+ sys/sysinfo.cpp
+ sys/sysinfo.hpp
+ sys/mutex.cpp
+ sys/mutex.hpp
+ sys/condition.cpp
+ sys/condition.hpp
+ sys/platform.cpp
+ sys/platform.hpp
+ ir/context.cpp
+ ir/context.hpp
+ ir/unit.cpp
+ ir/unit.hpp
+ ir/constant.cpp
+ ir/constant.hpp
+ ir/instruction.cpp
+ ir/instruction.hpp
+ ir/register.cpp
+ ir/register.hpp
+ ir/function.cpp
+ ir/function.hpp)
-set (COMPILE_UTEST false CACHE bool "Compile or not the unit tests")
-if (COMPILE_UTEST)
- set (GBE_SRC ${GBE_SRC})
-endif (COMPILE_UTEST)
+ if (GBE_COMPILE_UTEST)
+ set (GBE_SRC ${GBE_SRC})
+ endif (GBE_COMPILE_UTEST)
+endif (GBE_USE_BLOB)
include_directories (.)
add_library (gbe SHARED ${GBE_SRC})
--- /dev/null
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file blob.cpp
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Compile the complete project from one file. This allows pretty aggresive
+ * optimization from the compiler and decreases the binary size
+ */
+
+#include "sys/assert.cpp"
+#include "sys/string.cpp"
+#include "sys/alloc.cpp"
+#include "sys/sysinfo.cpp"
+#include "sys/mutex.cpp"
+#include "sys/condition.cpp"
+#include "sys/platform.cpp"
+#include "ir/context.cpp"
+#include "ir/unit.cpp"
+#include "ir/constant.cpp"
+#include "ir/instruction.cpp"
+#include "ir/register.cpp"
+#include "ir/function.cpp"
+
+#if GBE_COMPILE_UTEST
+
+#endif /* GBE_COMPILE_UTEST */
+
/**
* \file alloc.cpp
- *
* \author Benjamin Segovia <benjamin.segovia@intel.com>
+ *
+ * Provides facilities to track allocations and pre-initialize memory at
+ * memory allocation and memory free time
*/
#include "sys/alloc.hpp"
#include "sys/atomic.hpp"
/*! Declare C like interface functions here */
static MemDebugger *memDebugger = NULL;
+
+ /*! Stop the memory debugger */
+ static void MemDebuggerEnd(void) {
+ MemDebugger *_debug = memDebugger;
+ memDebugger = NULL;
+ delete _debug;
+ }
+
+ /*! Use this to serialize multiple starts of the debugger */
+ static MutexSys startMemDebuggerMutex;
+
+ /*! Start the memory debugger */
+ static void MemDebuggerStart(void) {
+ Lock<MutexSys> lock(startMemDebuggerMutex);
+ if (memDebugger == NULL) {
+ atexit(MemDebuggerEnd);
+ memDebugger = new MemDebugger;
+ }
+ }
+
void* MemDebuggerInsertAlloc(void *ptr, const char *file, const char *function, int line) {
- if (memDebugger) return memDebugger->insertAlloc(ptr, file, function, line);
- return ptr;
+ if (memDebugger == NULL) MemDebuggerStart();
+ return memDebugger->insertAlloc(ptr, file, function, line);
}
void MemDebuggerRemoveAlloc(void *ptr) {
- if (memDebugger) memDebugger->removeAlloc(ptr);
+ if (memDebugger == NULL) MemDebuggerStart();
+ memDebugger->removeAlloc(ptr);
}
void MemDebuggerDumpAlloc(void) {
- if (memDebugger) memDebugger->dumpAlloc();
+ if (memDebugger == NULL) MemDebuggerStart();
+ memDebugger->dumpAlloc();
}
void MemDebuggerEnableMemoryInitialization(bool enabled) {
memoryInitializationEnabled = enabled;
void MemDebuggerInitializeMem(void *mem, size_t sz) {
if (memoryInitializationEnabled) std::memset(mem, 0xcd, sz);
}
- void MemDebuggerStart(void) {
- if (memDebugger) MemDebuggerEnd();
- memDebugger = new MemDebugger;
- }
- void MemDebuggerEnd(void) {
- MemDebugger *_debug = memDebugger;
- memDebugger = NULL;
- delete _debug;
- }
+} /* namespace gbe */
+
#endif /* GBE_DEBUG_MEMORY */
-}
namespace gbe
{
+#if GBE_DEBUG_MEMORY
void* malloc(size_t size) {
- void *ptr = std::malloc(size);
- MemDebuggerInitializeMem(ptr, size);
- return ptr;
+ void *ptr = std::malloc(size + sizeof(size_t));
+ *(size_t *) ptr = size;
+ MemDebuggerInitializeMem((char*) ptr + sizeof(size_t), size);
+ return (char *) ptr + sizeof(size_t);
+ }
+ void free(void *ptr) {
+ if (ptr != NULL) {
+ char *toFree = (char*) ptr - sizeof(size_t);
+ const size_t size = *(size_t *) toFree;
+ MemDebuggerInitializeMem(ptr, size);
+ std::free(toFree);
+ }
}
+#else
+ void* malloc(size_t size) { return std::malloc(size); }
+ void free(void *ptr) { if (ptr != NULL) std::free(ptr); }
+#endif /* GBE_DEBUG_MEMORY */
+
+} /* namespace gbe */
- void* realloc(void *ptr, size_t size) {
#if GBE_DEBUG_MEMORY
- if (ptr) MemDebuggerRemoveAlloc(ptr);
-#endif /* GBE_DEBUG_MEMORY */
- GBE_ASSERT(size);
- if (ptr == NULL) {
- ptr = std::realloc(ptr, size);
+
+namespace gbe
+{
+ void* alignedMalloc(size_t size, size_t align) {
+ void* mem = malloc(size+(align-1)+sizeof(uintptr_t) + sizeof(void*));
+ FATAL_IF (!mem && size, "memory allocation failed");
+ char* aligned = (char*) mem + sizeof(uintptr_t) + sizeof(void*);
+ aligned += align - ((uintptr_t)aligned & (align - 1));
+ ((void**)aligned)[-1] = mem;
+ ((uintptr_t*)aligned)[-2] = uintptr_t(size);
+ MemDebuggerInitializeMem(aligned, size);
+ return aligned;
+ }
+
+ void alignedFree(void* ptr) {
+ if (ptr) {
+ const size_t size = ((uintptr_t*)ptr)[-2];
MemDebuggerInitializeMem(ptr, size);
- return ptr;
- } else
- return std::realloc(ptr, size);
+ free(((void**)ptr)[-2]);
+ }
}
+} /* namespace gbe */
- void free(void *ptr) { if (ptr != NULL) std::free(ptr); }
-}
+#else
////////////////////////////////////////////////////////////////////////////////
/// Windows Platform
////////////////////////////////////////////////////////////////////////////////
-#ifdef __WIN32__
+#if defined(__WIN32__)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
return ptr;
}
- void alignedFree(void *ptr) { _mm_free(ptr); }
-}
-#endif
+ void alignedFree(void *ptr) { if (ptr) _mm_free(ptr); }
+} /* namespace gbe */
+
+#endif /* __WIN32__ */
////////////////////////////////////////////////////////////////////////////////
/// Linux Platform
////////////////////////////////////////////////////////////////////////////////
-#ifdef __LINUX__
+#if defined(__LINUX__)
#include <unistd.h>
#include <sys/mman.h>
return ptr;
}
- void alignedFree(void *ptr) { free(ptr); }
-}
+ void alignedFree(void *ptr) { if (ptr) free(ptr); }
+} /* namespace gbe */
-#endif
+#endif /* __LINUX__ */
////////////////////////////////////////////////////////////////////////////////
/// MacOS Platform
////////////////////////////////////////////////////////////////////////////////
-#ifdef __MACOSX__
+#if defined(__MACOSX__)
#include <cstdlib>
return aligned;
}
- void alignedFree(void* ptr) {
- GBE_ASSERT(ptr);
- free(((void**)ptr)[-1]);
- }
-}
+ void alignedFree(void* ptr) { if (ptr) free(((void**)ptr)[-1]); }
+} /* namespace gbe */
+
+#endif /* __MACOSX__ */
-#endif
+#endif /* GBE_DEBUG_MEMORY */
/**
* \file alloc.hpp
- *
* \author Benjamin Segovia <benjamin.segovia@intel.com>
*/
#ifndef __GBE_ALLOC_HPP__
{
/*! regular allocation */
void* malloc(size_t size);
- void* realloc(void *ptr, size_t size);
void free(void *ptr);
/*! Aligned allocation */
void MemDebuggerDumpAlloc(void);
void MemDebuggerInitializeMem(void *mem, size_t sz);
void MemDebuggerEnableMemoryInitialization(bool enabled);
- void MemDebuggerStart(void);
- void MemDebuggerEnd(void);
#else
INLINE void* MemDebuggerInsertAlloc(void *ptr, const char*, const char*, int) {return ptr;}
INLINE void MemDebuggerRemoveAlloc(void *ptr) {}
INLINE void MemDebuggerDumpAlloc(void) {}
INLINE void MemDebuggerInitializeMem(void *mem, size_t sz) {}
INLINE void MemDebuggerEnableMemoryInitialization(bool enabled) {}
- INLINE void MemDebuggerStart(void) {}
- INLINE void MemDebuggerEnd(void) {}
#endif /* GBE_DEBUG_MEMORY */
/*! Properly handle the allocated type */
/*! Declare a class with custom allocators */
#define GBE_CLASS(TYPE) \
-public: \
+public: \
GBE_STRUCT(TYPE) \
private:
/*! Declare an aligned structure */
-#define GBE_ALIGNED_STRUCT(ALIGN) \
- void* operator new(size_t size) { return gbe::alignedMalloc(size, ALIGN); }\
- void* operator new[](size_t size) { return gbe::alignedMalloc(size, ALIGN); }\
- void operator delete(void* ptr) { gbe::alignedFree(ptr); } \
+#define GBE_ALIGNED_STRUCT(ALIGN) \
+ void* operator new(size_t size) { return gbe::alignedMalloc(size, ALIGN); } \
+ void* operator new[](size_t size) { return gbe::alignedMalloc(size, ALIGN); } \
+ void operator delete(void* ptr) { gbe::alignedFree(ptr); } \
void operator delete[](void* ptr) { gbe::alignedFree(ptr); }
/*! Declare an aligned class */
#define GBE_ALIGNED_CLASS(ALIGN) \
-public: \
+public: \
GBE_ALIGNED_STRUCT(ALIGN) \
private:
#define GBE_MALLOC(SZ) \
gbe::MemDebuggerInsertAlloc(gbe::malloc(SZ),__FILE__, __FUNCTION__, __LINE__)
-#define GBE_REALLOC(PTR, SZ) \
- gbe::MemDebuggerInsertAlloc(gbe::realloc(PTR, SZ),__FILE__, __FUNCTION__, __LINE__)
-
#define GBE_FREE(X) \
do { gbe::MemDebuggerRemoveAlloc(X); gbe::free(X); } while (0)
--- /dev/null
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file assert.cpp
+ *
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#if GBE_COMPILE_UTESTS
+
+#include "assert.hpp"
+#include "exception.hpp"
+namespace gbe
+{
+ void onFailedAssert(const char *file, const char *fn, int line)
+ {
+ char lineString[256];
+ sprintf(lineString, "%i", line);
+ const std::string msg = "file " + std::string(file)
+ + ", function " + std::string(fn)
+ + ", line " + std::string(lineString);
+ throw Exception(msg);
+ }
+} /* namespace gbe */
+
+#else
+
+#include "sys/assert.hpp"
+#include "sys/exception.hpp"
+#include "sys/platform.hpp"
+#include <cstdio>
+
+namespace gbe
+{
+ void onFailedAssert(const char *file, const char *fn, int32_t line)
+ {
+ fprintf(stderr, " ASSERTION FAILED: file %s, function %s, line %i\n",
+ file, fn, line);
+ fflush(stdout);
+ DEBUGBREAK();
+ _exit(-1);
+ }
+} /* namespace gbe */
+
+#endif /* GBE_COMPILE_UTESTS */
+
--- /dev/null
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+
+/**
+ * \file assert.hpp
+ *
+ * \author Benjamin Segovia <benjamin.segovia@intel.com>
+ */
+#ifndef __GBE_ASSERT_HPP__
+#define __GBE_ASSERT_HPP__
+
+namespace gbe
+{
+ /*! To ensure that condition truth. Optional message is supported */
+ void onFailedAssert(const char *file, const char *fn, int line);
+} /* namespace gbe */
+
+#endif /* __GBE_ASSERT_HPP__ */
+
#ifndef __GBE_EXCEPTION_HPP__
#define __GBE_EXCEPTION_HPP__
-#if defined(GBE_COMPILE_UTEST)
+#if GBE_COMPILE_UTESTS
#include "string.hpp"
#include <exception>
} /* namespace gbe */
-#endif /* GBE_COMPILE_UTEST */
+#endif /* GBE_COMPILE_UTESTS */
#endif /* __GBE_EXCEPTION_HPP__ */
/**
* \file vector.hpp
- *
* \author Benjamin Segovia <benjamin.segovia@intel.com>
*/