-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMode.cpp
410 lines (328 loc) · 11 KB
/
Mode.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "std.h"
#include "rope.h"
#include "Mode.h"
#include "lock.h"
#include "SpmtThread.h"
#include "RopeVM.h"
#include "DebugScaffold.h"
#include "Helper.h"
#include "Loggers.h"
#include "jni.h"
#include "Message.h"
using namespace std;
Mode::Mode(const char* name)
:
pc(0),
frame(0),
sp(0),
m_name(name)
{
}
const char*
Mode::get_name()
{
return m_name;
}
void
Mode::reset_context()
{
pc = 0;
frame = 0;
sp = 0;
}
void
Mode::set_st(SpmtThread* st)
{
m_st = st;
}
void
Mode::before_alloc_object()
{
}
void
Mode::after_alloc_object(Object* new_object)
{
//if (RopeVM::graph_enabled and is_client_code) {
if (RopeVM::graph_enabled) {
if (is_app_obj(new_object)) {
ofs_timeline << type_name(new_object) << " " << new_object << " created" << endl;
}
}
SpmtThread* st = m_st->get_thread()->assign_spmt_thread_for(new_object);
// assign_spmt_thread_for后要么跟set_st,要么跟join_st_in_other_threads
new_object->set_st(st);
}
void
Mode::send_msg(Message* msg)
{
assert(false);
}
// 在确定模式下处理确定消息,在推测模式下处理推测消息。
void
Mode::process_msg(Message* msg)
{
// if (debug_scaffold::is_client_code &&
// m_st->is_certain_mode()) {
// m_st->m_count_certain_instr++;
// Statistic::instance()->probe_instr_exec(0);
// }
MsgType type = msg->get_type();
if (type == MsgType::INVOKE) {
InvokeMsg* invoke_msg = static_cast<InvokeMsg*>(msg);
assert(not invoke_msg->is_top()); // 顶级方法不在这里处理
// 为invoke_msg指定的方法及参数创建栈帧并设置
invoke_impl(invoke_msg->get_target_object(),
invoke_msg->mb, &invoke_msg->parameters[0],
invoke_msg->get_source_st(),
invoke_msg->caller_pc,
invoke_msg->caller_frame,
invoke_msg->caller_sp, false);
}
else if (type == MsgType::PUT) {
PutMsg* put_msg = static_cast<PutMsg*>(msg);
// 向put_msg指定的字段写入一个值
uintptr_t* field_addr = put_msg->get_field_addr();
for (auto i = 0u; i < put_msg->val.size(); ++i) {
write(field_addr + i, put_msg->val[i]);
}
// 构造put_ret_msg作为回复
PutRetMsg* put_ret_msg = new PutRetMsg(put_msg->get_source_st());
send_msg(put_ret_msg);
// 需要加载下一条待处理消息
m_st->m_spec_running_state = RunningState::ongoing_but_need_launch_new_msg;
}
else if (type == MsgType::GET) {
GetMsg* get_msg = static_cast<GetMsg*>(msg);
// 从get_msg指定的字段读出一个值
uintptr_t* field_addr = get_msg->get_field_addr();
int field_size = get_msg->get_field_size();
std::vector<uintptr_t> value;
for (int i = 0; i < field_size; ++i) {
value.push_back(read(field_addr + i));
}
// 构造get_ret_msg作为回复
GetRetMsg* get_ret_msg = new GetRetMsg(get_msg->get_source_st(),
&value[0], value.size());
send_msg(get_ret_msg);
// 需要加载下一条待处理消息
m_st->m_spec_running_state = RunningState::ongoing_but_need_launch_new_msg;
}
else if (type == MsgType::ASTORE) {
AStoreMsg* astore_msg = static_cast<AStoreMsg*>(msg);
// 向astore_msg指定的数组的指定位置处写入一个值
Object* array = astore_msg->get_target_object();
int index = astore_msg->index;
int type_size = astore_msg->type_size;
store_to_array_from_c(&astore_msg->val[0],
array, index, type_size);
// 构造astore_ret_msg作为回复
AStoreRetMsg* astore_ret_msg = new AStoreRetMsg(astore_msg->get_source_st());
send_msg(astore_ret_msg);
// 需要加载下一条待处理消息
m_st->m_spec_running_state = RunningState::ongoing_but_need_launch_new_msg;
}
else if (type == MsgType::ALOAD) {
ALoadMsg* aload_msg = static_cast<ALoadMsg*>(msg);
// 从aload_msg指定的数组的指定位置处读出一个值
Object* array = aload_msg->get_target_object();
int index = aload_msg->index;
int type_size = aload_msg->type_size;
std::vector<uintptr_t> value(2); // at most 2 slots
load_from_array_to_c(&value[0],
array, index, type_size);
// 构造aload_ret_msg作为回复
ALoadRetMsg* aload_ret_msg = new ALoadRetMsg(aload_msg->get_source_st(),
&value[0], size2nslots(type_size));
send_msg(aload_ret_msg);
// 需要加载下一条待处理消息
m_st->m_spec_running_state = RunningState::ongoing_but_need_launch_new_msg;
}
else if (type == MsgType::RETURN) {
ReturnMsg* return_msg = static_cast<ReturnMsg*>(msg);
// 方法重入会导致这些东西改变,所以需要按消息中的重新设置。
pc = return_msg->caller_pc;
frame = return_msg->caller_frame;
sp = return_msg->caller_sp;
// 将return_msg携带的返回值写入ostack
for (auto i : return_msg->retval) {
write(sp++, i);
}
pc += (*pc == OPC_INVOKEINTERFACE_QUICK ? 5 : 3);
}
else if (type == MsgType::PUT_RET) {
//PutRetMsg* put_ret_msg = static_cast<PutRetMsg*>(msg);
pc += 3;
}
else if (type == MsgType::GET_RET) {
GetRetMsg* get_ret_msg = static_cast<GetRetMsg*>(msg);
// 将get_ret_msg携带的值写入ostack
for (auto i : get_ret_msg->val) {
write(sp++, i);
}
pc += 3;
}
else if (type == MsgType::ASTORE_RET) {
//AStoreRetMsg* astore_ret_msg = static_cast<AStoreRetMsg*>(msg);
pc += 1;
}
else if (type == MsgType::ALOAD_RET) {
ALoadRetMsg* aload_ret_msg = static_cast<ALoadRetMsg*>(msg);
// 将aload_ret_msg携带的值写入ostack
for (auto i : aload_ret_msg->val) {
write(sp++, i);
}
pc += 1;
}
else {
assert(false);
}
}
/*
注意:在以下这些方法的实现中谨记:
元素在数组中是紧致存储的,几个字节就是几个字节;
而在ostack和c内存不足4字节,也要占用4字节。
所以,涉及到数组的读写,是几个字节就几个字节。
而涉及到ostack或c内存的读写,要么是4字节,要么是8字节。
(这点从强制类型转换即可看出)
*/
// 根据模式从数组读数值,根据模式写入ostack
void
Mode::load_from_array(uintptr_t* sp, Object* array, int index, int type_size)
{
void* elem_addr = array_elem_addr(array, index, type_size);
if (type_size == 1) {
write(sp, read((uint8_t*)elem_addr));
}
else if (type_size == 2) {
write(sp, read((uint16_t*)elem_addr));
}
else if (type_size == 4) {
write(sp, read((uint32_t*)elem_addr));
}
else if (type_size == 8) {
write((uint64_t*)sp, read((uint64_t*)elem_addr));
}
else {
assert(false);
}
}
// 根据模式从ostack读数值,根据模式写入数组
void
Mode::store_to_array(uintptr_t* sp, Object* array, int index, int type_size)
{
void* elem_addr = array_elem_addr(array, index, type_size);
if (type_size == 1) {
write((uint8_t*)elem_addr, (uint32_t)read(sp));
}
else if (type_size == 2) {
write((uint16_t*)elem_addr, (uint32_t)read(sp));
}
else if (type_size == 4) {
write((uint32_t*)elem_addr, (uint32_t)read(sp));
}
else if (type_size == 8) {
write((uint64_t*)elem_addr, read((uint64_t*)sp));
}
else {
assert(false);
}
}
// 根据模式从数组读数值,写入c内存
void
Mode::load_from_array_to_c(uintptr_t* sp, Object* array, int index, int type_size)
{
void* elem_addr = array_elem_addr(array, index, type_size);
if (type_size == 1) {
*sp = read((uint8_t*)elem_addr);
}
else if (type_size == 2) {
*sp = read((uint16_t*)elem_addr);
}
else if (type_size == 4) {
*sp = read((uint32_t*)elem_addr);
}
else if (type_size == 8) {
*(uint64_t*)sp = read((uint64_t*)elem_addr);
}
else {
assert(false);
}
}
// 从c内存中读数值,根据模式写入数组
void
Mode::store_to_array_from_c(uintptr_t* sp, Object* array, int index, int type_size)
{
void* elem_addr = array_elem_addr(array, index, type_size);
if (type_size == 1) {
write((uint8_t*)elem_addr, (uint32_t)*sp);
}
else if (type_size == 2) {
write((uint16_t*)elem_addr, (uint32_t)*sp);
}
else if (type_size == 4) {
write((uint32_t*)elem_addr, (uint32_t)*sp);
}
else if (type_size == 8) {
write((uint64_t*)elem_addr, *(uint64_t*)sp);
}
else {
assert(false);
}
}
void
g_load_from_stable_array_to_c(uintptr_t* sp, Object* array, int index, int type_size)
{
void* elem_addr = array_elem_addr(array, index, type_size);
if (type_size == 1) {
*sp = *(uint8_t*)elem_addr;
}
else if (type_size == 2) {
*sp = *(uint16_t*)elem_addr;
}
else if (type_size == 4) {
*sp = *(uint32_t*)elem_addr;
}
else if (type_size == 8) {
*(uint64_t*)sp = *(uint64_t*)elem_addr;
}
else {
assert(false);
}
}
void
Mode::invoke_impl(Object* target_object, MethodBlock* new_mb, uintptr_t* args,
SpmtThread* caller, CodePntr caller_pc, Frame* caller_frame, uintptr_t* caller_sp,
bool is_top)
{
frame = push_frame(target_object, new_mb, args,
caller, caller_pc, caller_frame, caller_sp,
is_top);
// 给同步方法加锁
if (frame->mb->is_synchronized()) {
Object *sync_ob = frame->mb->is_static() ?
frame->mb->classobj : (Object*)frame->lvars[0]; // lvars[0] is 'this' reference
objectLock(sync_ob);
}
// 如果是native方法,在此处立即执行其native code
if (new_mb->is_native()) {
// 将参数复制到ostack。push_frame只把参数复制到了lvars。native方法比较特殊。
if (args)
std::copy(args, args + new_mb->args_count, frame->ostack_base);
// 写清楚之后应该是这样:
// typedef uintptr_t* EXAMPLE(Class*, MethodBlock*, uintptr_t*);
// sp = (*(EXAMPLE*)new_mb->native_invoker)();
sp = (*(uintptr_t *(*)(Class*, MethodBlock*, uintptr_t*))
new_mb->native_invoker)(new_mb->classobj, new_mb,
frame->ostack_base);
if (m_st->get_thread()->exception) {
throw_exception;
}
else {
// native方法已经结束,返回值已经产生。该返回了。
do_method_return(sp - frame->ostack_base);
}
return;
}
sp = frame->ostack_base;
pc = (CodePntr)frame->mb->code;
}