forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLVal.cpp
More file actions
140 lines (111 loc) · 3.43 KB
/
MLVal.cpp
File metadata and controls
140 lines (111 loc) · 3.43 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "MLVal.h"
#include <string.h>
extern "C" {
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/alloc.h>
}
#include <string>
void init_ml() {
static bool initialized = false;
// Batteries is unhappy if Sys.argv.(0) is NULL or uninitialized
std::string fake_exe_name = "<halide_embedded>";
if (!initialized) {
char *fake_argv[2] = {&fake_exe_name[0], NULL};
caml_startup(fake_argv);
initialized = true;
}
}
struct MLVal::Contents {
Contents() : val(Val_unit) {
register_global_root(&val);
}
Contents(value v) : val(v) {
register_global_root(&val);
}
~Contents() {
remove_global_root(&val);
}
value val;
};
MLVal MLVal::find(const char *name) {
init_ml();
MLVal v;
value *result = caml_named_value(name);
if (!result) {
printf("%s not found!\n", name);
exit(1);
}
v.contents.reset(new Contents(*result));
return v;
}
MLVal MLValFromValue(value v) {
if (Is_exception_result(v)) {
printf("Can't make an MLVal from an exception!\n");
exit(1);
}
return MLVal((void *)v);
}
void *MLVal::asVoidPtr() const {
return (void *)(contents->val);
}
MLVal::MLVal(int x) : contents(new Contents(Val_int(x))) {
}
MLVal::MLVal(uint32_t x) : contents(new Contents(Val_int(x))) {
}
MLVal::MLVal(float x) {
init_ml();
contents.reset(new Contents(caml_copy_double((double)x)));
}
MLVal::MLVal(double x) {
init_ml();
contents.reset(new Contents(caml_copy_double(x)));
}
MLVal::MLVal(const char *str) {
init_ml();
value v = caml_alloc_string(strlen(str));
strcpy(String_val(v), str);
contents.reset(new Contents(v));
}
MLVal::MLVal(const std::string &str) {
init_ml();
value v = caml_alloc_string(str.size());
strcpy(String_val(v), &str[0]);
contents.reset(new Contents(v));
}
MLVal::operator std::string() {
return std::string(String_val(contents->val));
}
MLVal::MLVal(void *ptr) : contents(new Contents((value)ptr)) {
}
MLVal::MLVal(const MLVal &other) : contents(other.contents) {
}
void MLVal::unpackPair(const MLVal &tuple, MLVal &first, MLVal &second) {
first = MLValFromValue(Field(tuple.contents->val, 0));
second = MLValFromValue(Field(tuple.contents->val, 1));
}
MLVal MLVal::operator()() const {
return MLValFromValue(caml_callback(contents->val, Val_unit));
}
MLVal MLVal::operator()(const MLVal &x) const {
return MLValFromValue(caml_callback(contents->val, x.contents->val));
}
MLVal MLVal::operator()(const MLVal &x, const MLVal &y) const {
return MLValFromValue(caml_callback2(contents->val, x.contents->val, y.contents->val));
}
MLVal MLVal::operator()(const MLVal &x, const MLVal &y, const MLVal &z) const {
return MLValFromValue(caml_callback3(contents->val,
x.contents->val,
y.contents->val,
z.contents->val));
}
MLVal MLVal::operator()(const MLVal &x, const MLVal &y, const MLVal &z, const MLVal &w) const {
return (*this)(x, y, z)(w);
}
MLVal MLVal::operator()(const MLVal &a, const MLVal &b, const MLVal &c, const MLVal &d, const MLVal &e) const {
return (*this)(a, b, c)(d, e);
}
MLVal MLVal::operator()(const MLVal &a, const MLVal &b, const MLVal &c, const MLVal &d, const MLVal &e, const MLVal &f) const {
return (*this)(a, b, c)(d, e, f);
}