-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.h
88 lines (70 loc) · 2.06 KB
/
functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef VAIVEN_HEADER_FUNCTIONS
#define VAIVEN_HEADER_FUNCTIONS
#include <map>
#include <memory>
#include <string>
#include "asmjit/src/asmjit/asmjit.h"
#include "value.h"
#include "function_usage.h"
#include "type_info.h"
#include "ast/funcdecl.h"
using std::map;
using std::string;
using std::unique_ptr;
namespace vaiven {
typedef Value (*OverkillFunc)(Value rdi, Value rsi, Value rdx, Value rcx, Value r8, Value r9, Value stack1, Value stack2);
class Function {
public:
Function(const Function& that) = delete;
Function(asmjit::JitRuntime* runtime,
int argc,
unique_ptr<FunctionUsage> usage,
ast::FuncDecl<>* ast)
: runtime(runtime), argc(argc), usage(std::move(usage)), ast(ast), isPure(false), isNative(false) {};
~Function() {
if (!isNative) {
runtime->release(fptr);
}
}
bool isNative;
int argc;
int worstSize;
bool isPure; // for native functions
OverkillFunc fptr;
OverkillFunc slowfptr;
unique_ptr<FunctionUsage> usage;
unique_ptr<ast::FuncDecl<> > ast;
private:
asmjit::JitRuntime* runtime;
};
class DuplicateFunctionError {
public:
DuplicateFunctionError(string name) : name(name) {};
string name;
};
class Functions {
public:
asmjit::JitRuntime runtime;
void addNative(string name, int argc, void* fptr, bool isPure) {
funcs[name] = unique_ptr<Function>(new Function(NULL, argc, unique_ptr<FunctionUsage>(), NULL));
funcs[name]->isNative = true;
funcs[name]->fptr = (OverkillFunc) fptr;
funcs[name]->isPure = isPure;
}
void prepareFunc(string name, int argc, unique_ptr<FunctionUsage> usage,
ast::FuncDecl<>* ast) {
if (funcs.find(name) != funcs.end()) {
throw DuplicateFunctionError(name);
}
funcs[name] = unique_ptr<Function>(new Function(&runtime, argc, std::move(usage), ast));
}
void finalizeFunc(string name, asmjit::CodeHolder* holder) {
int worstSize = holder->getCodeSize();
runtime.add(&funcs[name]->fptr, holder);
funcs[name]->worstSize = worstSize;
}
// private:
map<string, unique_ptr<Function> > funcs;
};
}
#endif