-
Notifications
You must be signed in to change notification settings - Fork 9
/
ayavm.cpp
218 lines (178 loc) · 4.82 KB
/
ayavm.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// AYA version 5
//
// AYAの1インスタンスを保持するクラスAYAVM
// written by the Maintenance Shop/C.Ponapalt 2006
//
#ifdef _MSC_VER
#pragma warning( disable : 4786 ) //「デバッグ情報内での識別子切捨て」
#pragma warning( disable : 4503 ) //「装飾された名前の長さが限界を越えました。名前は切り捨てられます。」
#endif
#include "ayavm.h"
#include "basis.h"
#include "file.h"
#include "function.h"
#include "lib.h"
#include "misc.h"
#include "parser0.h"
#include "parser1.h"
#include "sysfunc.h"
//#include "babel/babel.h"
#ifdef POSIX
#include <sys/time.h>
#else
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#endif
//////////DEBUG/////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
////////////////////////////////////////
/*-----------------------------------------------
コンストラクタ
-----------------------------------------------*/
CAyaVM::CAyaVM()
{
memset(&rs_sysfunc64, 0, sizeof(rs_sysfunc64));
memset(&rs_internal64, 0, sizeof(rs_internal64));
}
/*-----------------------------------------------
コピーコンストラクタ
shared_ptrをディープコピーする点に注意
-----------------------------------------------*/
CAyaVM::CAyaVM(CAyaVM &ovm)
{
#define copy_new(name) yaya::shared_ptr_deep_copy(ovm.name,name);
copy_new(m_basis);
copy_new(m_function_exec);
copy_new(m_gdefines);
copy_new(m_call_limit);
copy_new(m_sysfunction);
copy_new(m_variable);
copy_new(m_files);
copy_new(m_libs);
copy_new(m_parser0);
copy_new(m_parser1);
#undef copy_new
m_logger = ovm.m_logger;
rs_sysfunc64 = ovm.rs_sysfunc64;
rs_internal64 = ovm.rs_internal64;
}
CAyaVM* CAyaVM::get_a_deep_copy()
{
CAyaVM *nvm = new CAyaVM(*this);
return nvm;
}
/*-----------------------------------------------
初期化
ほぼ乱数初期化用
-----------------------------------------------*/
void CAyaVM::load(void)
{
unsigned int dwSeed;
//babel::init_babel();
#ifdef POSIX
struct timeval tv;
gettimeofday(&tv,NULL);
dwSeed = tv.tv_usec;
#else
dwSeed = ::GetTickCount();
#endif
init_genrand64(rs_internal64, dwSeed);
init_genrand64(rs_sysfunc64, dwSeed);
}
/*-----------------------------------------------
終了
-----------------------------------------------*/
void CAyaVM::unload(void)
{
}
/*-----------------------------------------------
request
-----------------------------------------------*/
void CAyaVM::request_before(void)
{
}
void CAyaVM::request_after(void)
{
m_function_destruct.reset();
}
/*-----------------------------------------------
パース用
-----------------------------------------------*/
CFunctionDef& CAyaVM::function_parse()
{
if ( m_function_parse.get() ) {
return *m_function_parse.get();
}
function_exec();
m_function_parse = m_function_exec;
return *m_function_parse.get();
}
/*-----------------------------------------------
内部func操作
-----------------------------------------------*/
void CAyaVM::func_parse_to_exec(void)
{
m_function_destruct = m_function_exec;
m_function_exec = m_function_parse;
}
void CAyaVM::func_parse_destruct(void)
{
m_function_parse.reset();
m_function_parse = m_function_exec;
}
void CAyaVM::func_parse_new(void)
{
yaya::shared_ptr_deep_copy(m_function_exec,m_function_parse);
m_function_parse->deep_copy_func(*m_function_exec);
}
/*-----------------------------------------------
乱数生成
-----------------------------------------------*/
size_t CAyaVM::genrand_uint(size_t n)
{
return genrand64_int63(rs_internal64) % n;
}
yaya::int_t CAyaVM::genrand_sysfunc_ll(yaya::int_t n)
{
return genrand64_int63(rs_sysfunc64) % n;
}
void CAyaVM::genrand_sysfunc_srand_ll(yaya::int_t n)
{
init_genrand64(rs_sysfunc64,n);
}
void CAyaVM::genrand_sysfunc_srand_array(const std::uint64_t a[],const int n)
{
init_by_array64(rs_sysfunc64,a,n);
}
// ちょっとひどいハックですが……
#define FACTORY_DEFINE_THIS(classt,deft) \
classt & CAyaVM:: deft () { \
if( m_ ## deft .get() == NULL ) { \
m_##deft = std_make_shared<classt >(classt(*this)); \
} \
return *(m_ ## deft .get()); \
}
#define FACTORY_DEFINE_PLAIN(classt,deft) \
classt & CAyaVM:: deft () { \
if( m_ ## deft .get() == NULL ) { \
m_##deft = std_make_shared<classt >(); \
} \
return *(m_ ## deft .get()); \
}
FACTORY_DEFINE_PLAIN(std::vector<CDefine>,gdefines)
FACTORY_DEFINE_THIS(CBasis,basis)
FACTORY_DEFINE_PLAIN(CFunctionDef,function_exec)
FACTORY_DEFINE_PLAIN(CCallLimit,call_limit)
FACTORY_DEFINE_THIS(CSystemFunction,sysfunction)
FACTORY_DEFINE_THIS(CGlobalVariable,variable)
FACTORY_DEFINE_PLAIN(CFile,files)
FACTORY_DEFINE_THIS(CLib,libs)
FACTORY_DEFINE_THIS(CParser0,parser0)
FACTORY_DEFINE_THIS(CParser1,parser1)