Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ project(tVM

set(CMAKE_CXX_STANDARD 14)

if (WIN32)
set(CMAKE_CXX_FLAGS "/EHsc")
endif()

add_subdirectory(tvm)
add_subdirectory(tvm-run)
5 changes: 5 additions & 0 deletions tvm-run/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include "tvm/ExecutionContext.hpp"
#include "tvm/BytecodeParser.hpp"

#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif

/// tvm-run's usage string. Printed when run with -help.
static const char* usage =
"Usage: tvm-run [<option>...] [--] <module> [<arg>...]\n"
Expand Down
5 changes: 5 additions & 0 deletions tvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ target_include_directories(tvm
PUBLIC
include/
)

target_compile_definitions(tvm
PRIVATE
TVM_DLL_EXPORTS
)
5 changes: 3 additions & 2 deletions tvm/include/tvm/BytecodeParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
#include <vector>

#include "instructions.hpp"
#include "tvm/Visibility.hpp"

namespace tvm {

struct ByteCodeParserException : public std::runtime_error {
struct TVM_API ByteCodeParserException : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* @brief class BytecodeParser parses tVM instructions in .bc files
*
*/
class BytecodeParser {
class TVM_API BytecodeParser {
public:
explicit BytecodeParser(bool verbose = false) noexcept : _verbose(verbose) {}

Expand Down
15 changes: 15 additions & 0 deletions tvm/include/tvm/Compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

#include "instructions.hpp"

#if defined(_WIN32)
#include <windows.h>
#else
#include <sys/mman.h>
#endif /* _WIN32 */
#include <vector>
#include <memory>
#include <cstring>
Expand All @@ -26,14 +30,25 @@ struct CodeCache {
*/
CodeCache(size_t size)
: size_(size) {

#if defined(_WIN32)
start_ = VirtualAlloc(nullptr, size_, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (start_ == nullptr)
throw CompilationException{"Failed to allocate code cache memory."};
#else
start_ = mmap(0, size_ , PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1, 0);
if (start_ == MAP_FAILED)
throw CompilationException{"Failed to allocate code cache memory."};
#endif /* _WIN32 */
}

~CodeCache() noexcept {
#if defined(_WIN32)
VirtualFree(start_, 0, MEM_RELEASE);
#else
munmap(start_, size_);
#endif /* _WIN32 */
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tvm/include/tvm/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define TVM_MODULE_HPP_

#include "tvm/instructions.hpp"
#include "tvm/Visibility.hpp"

#include <cstdint>
#include <iostream>
Expand All @@ -11,7 +12,7 @@
namespace tvm {

/// An interpreter module.
struct Module {
struct TVM_API Module {
std::vector<Instruction> instructions;
};

Expand Down
7 changes: 4 additions & 3 deletions tvm/include/tvm/VirtualMachine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "tvm/OperandStack.hpp"
#include "tvm/instructions.hpp"
#include "tvm/Compiler.hpp"
#include "tvm/Visibility.hpp"

#include <cstring>
#include <ostream>
Expand All @@ -17,7 +18,7 @@ namespace tvm {
class ExecutionContext;
class VirtualMachine;

struct Config {
struct TVM_API Config {
bool debug = false; //< Enable debug code
bool verbose = false; //< Enable verbose printing and tracing
bool jit = false;
Expand All @@ -30,13 +31,13 @@ inline std::ostream &operator<<(std::ostream &out, const Config &cfg) {
return out;
}

struct InterpreterException : public std::runtime_error {
struct TVM_API InterpreterException : public std::runtime_error {
using std::runtime_error::runtime_error;
};

typedef StackElement (* JittedCode)();

class VirtualMachine {
class TVM_API VirtualMachine {
public:
VirtualMachine(const Config &cfg);

Expand Down
20 changes: 20 additions & 0 deletions tvm/include/tvm/Visibility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://gcc.gnu.org/wiki/Visibility
// Generic helper definitions for shared library support
#if defined __CYGWIN__ || defined _WIN32
#define TVM_DLL_IMPORT __declspec(dllimport)
#define TVM_DLL_EXPORT __declspec(dllexport)
#else
#if __GNUC__ >= 4
#define TVM_DLL_IMPORT __attribute__((visibility("default")))
#define TVM_DLL_EXPORT __attribute__((visibility("default")))
#else
#define TVM_DLL_IMPORT
#define TVM_DLL_EXPORT
#endif
#endif

#ifdef TVM_DLL_EXPORTS
#define TVM_API TVM_DLL_EXPORT
#else
#define TVM_API TVM_DLL_IMPORT
#endif
1 change: 1 addition & 0 deletions tvm/include/tvm/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstdint>
#include <ostream>
#include <string>
#include <unordered_map>

namespace tvm {
Expand Down