-
Notifications
You must be signed in to change notification settings - Fork 17
/
VM.cpp
621 lines (555 loc) · 15.5 KB
/
VM.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "VM.h"
#include <assert.h>
#include "Value.h"
#include "State.h"
#include "Function.h"
#include "Stack.h"
#include "libs/BaseLib.h"
VM::VM(State* state)
: _state(state),
_stackClosure(new Stack(20)),
_curInsVal(nullptr)
{
_stack = _state->getStack();
_state->setVM(this);
}
VM::~VM()
{
}
void VM::execute()
{
}
void VM::execute_frame()
{
}
int VM::runCode(InstructionValue* insSetVal)
{
//local a = 1 一条典型的初始化语句产生的指令有3条:OpCode_Push(1) OpCode_SetLocalVar (a) OpCode_Assign (=)
//a = 1 一条典型的赋值语句产生的指令有3条:OpCode_Push(1) OpCode_SetLocalVar (a) OpCode_InitLocalVar (=)
//一个函数声明语句也是3条指令:OpCode_GenerateClosure(1) OpCode_SetLocalVar (funcName) OpCode_InitLocalVar (=)
//引用一个值的命令是OpCode_GetLocalVar,它会压入该变量的值(从闭包的table值中查找)到栈中
//函数返回几个值,就会压入几个值到栈中,OpCode_Ret指令只是告诉闭包返回值的个数
/*调用函数参数时,没有特别的指令,只是OpCode_GetLocalVar压入值到栈。执行有参数的函数时,首先就是OpCode_SetLocalVar压入参数名,
然后就是OpCode_PassFunParam指令将参数名赋值为栈上的值
*/
//TODO
/*垃圾回收的基本思路就是:闭包退出时,遍历当前的table,看哪些不在栈上的就delete掉。但是这样有个问题,如果将一个变量传给一个函数,这样的话,闭包结束时,这个参数也会delete掉,
所以加一条规则:则检测闭包中的变量是否在外层引用着。当然闭包内部引用到外层的变量是不会出现这个情况的,因为此时该值不会在当前闭包table中记录
*/
//看能不能简化OpCode_Push(1),OpCode_SetLocalVar(a),OpCode_Assign(=)为两条指令
insSetVal->setParent(_curInsVal);
_curInsVal = insSetVal;
auto vtIns = insSetVal->getInstructionSet()->toVtInstructions();
for (auto it = vtIns.begin(); it != vtIns.end(); ++it) {
if (insSetVal->getBreaked()) {
printf("breaked!!!\n");
return 0;
}
Instruction* ins = *it;
switch (ins->op_code)
{
case Instruction::OpCode_AddGlobalTable:
add_global_table();
break;
case Instruction::OpCode_EnterClosure: //进入函数时调用
enterClosure();
break;
case Instruction::OpCode_QuitClosure:
quitClosure();
break;
case Instruction::OpCode_InitLocalVar: //局部变量声明
initLocalVar(ins);
break;
case Instruction::OpCode_GetLocalVar:
getLoacalVar(ins);
break;
case Instruction::OpCode_SetLocalVar:
setLoacalVar(ins);
break;
case Instruction::OpCode_GenerateClosure:
generateClosure(ins);
break;
case Instruction::OpCode_PassFunParam:
passFunParam(ins);
break;
case Instruction::OpCode_Call:
call(ins);
break;
case Instruction::OpCode_Ret:
funcionRet(ins);
break;
case Instruction::OpCode_Push:
pushValue(ins);
break;
case Instruction::OpCode_Assign:
assignOperate(ins);
break;
case Instruction::OpCode_Plus:
case Instruction::OpCode_Minus:
case Instruction::OpCode_Multiply:
case Instruction::OpCode_Divide:
operateNum(ins);
break;
case Instruction::OpCode_If:
ifCompare(ins);
break;
case Instruction::OpCode_NumericFor:
numericFor(ins);
break;
case Instruction::OpCode_GenericFor:
genericFor(ins);
break;
case Instruction::OpCode_Break:
return -1;
case Instruction::OpCode_Less:
case Instruction::OpCode_Greater:
case Instruction::OpCode_LessEqual:
case Instruction::OpCode_GreaterEqual:
case Instruction::OpCode_NotEqual:
case Instruction::OpCode_Equal:
operateLogic(ins);
break;
case Instruction::OpCode_EnterBlock:
enterBlock(ins);
break;
case Instruction::OpCode_QuitBlock:
quitBlock(ins);
break;
case Instruction::OpCode_GenerateBlock:
generateBlock(ins);
break;
case Instruction::OpCode_TableDefine:
tableDefine(ins);
break;
case Instruction::OpCode_TableMemAccess:
tableAccess(ins);
break;
case Instruction::OpCode_TableArrIndex:
tableArrIndex(ins);
break;
case Instruction::OpCode_Negative:
negNumber(ins);
break;
case Instruction::OpCode_Length:
lenOfVale(ins);
break;
default:
break;
}
}
return 0;
}
void VM::generateClosure(Instruction* ins)
{
assert(ins->param.value->Type() == Value::TYPE_FUNCTION);
Function *func = static_cast<Function *>(ins->param.value);
Closure *cl = func->generateClosure(_state) ;
cl->setParentClosure(getCurrentClosure());
_stack->Push(cl);
}
void VM::add_global_table()
{
}
void VM::call(Instruction* ins)
{
int paramNum = ins->param.counter.counter1; //实参个数
Value* callee = _stack->popValue();
if (callee->Type() == Value::TYPE_NIL) {
printf("error, call a nil val");
}
if (callee->Type() == Value::TYPE_NATIVE_FUNCTION) {
((NativeFunc*)callee)->doCall(_state, (void*)paramNum);
}
else if (callee->Type() == Value::TYPE_CLOSURE) {
Closure* cl = static_cast<Closure*>(callee)->clone(); //这里必须克隆,,因为有递归调用函数
_stackClosure->Push(cl);
cl->setActParamNum(paramNum);
int needRetNum = ins->param.counter.counter2;
cl->setNeedRetNum(needRetNum);
runCode(cl->getPrototype());
}
}
void VM::enterClosure()
{
getCurrentClosure()->initClosure();
}
void VM::quitClosure()
{
Closure* cl = getCurrentClosure();
cl->clearClosure();
cl->balanceStack();
_stackClosure->popValue();
}
Closure* VM::getCurrentClosure()
{
if (_stackClosure->Size() == 0) {
return nullptr;
}
Closure* cl = (Closure*)_stackClosure->Top()->param.value;
assert(cl->Type() == Value::TYPE_CLOSURE);
return cl;
}
void VM::passFunParam(Instruction* ins) //其实这个也可以用OpCode_InitLocalVar代替
{
int needParamNum = ins->param.counter.counter1;
int actParamNum = getCurrentClosure()->getActParamNum();
assignVals(needParamNum, actParamNum, 0); //如果压入的值比参数多,后面会处理掉
}
void VM::assignVals(int num_key, int num_val, int type) //函数调用传入参数时也会调用这里
{
Table* tab = getCurrentClosure()->getTopTable();
std::list<Value*> listKeys;
std::list<Value*> listVals;
for (int i = 0; i < num_key; i++) {
listKeys.push_front(_stack->popValue());
}
if (num_key > num_val) { //值少于key,有可能是函数引起的a,b =f(),最多弹出key的个数,因为栈上可能有其他地方的值
num_val = (unsigned int)num_key < _stack->Size() ? num_key : _stack->Size();
}
for (int i = 0; i < num_val; i++) { //要把剩下的值全部弹出
Value* val = _stack->popValue();
listVals.push_front(val);
}
num_val = listVals.size();
if (num_key > num_val) { //a,b,c = 1, 2
for (int i = 0; i < num_key - num_val; i++) {
listVals.push_back(new Nil());
}
}
while (!listKeys.empty() && !listVals.empty()) { //有可能有多余的参数被舍弃
Value* key = listKeys.front();
Value* val = listVals.front();
if (type == 1) {
if (getCurrentClosure()->findUpTables(key, nullptr, &tab) == -1) { //赋值时没有找到就放在全局表中
tab = _state->getGlobalTable();
}
}
tab->Assign(key, val);
listKeys.pop_front();
listVals.pop_front();
}
}
void VM::initLocalVar(Instruction* ins)
{
int num_key = ins->param.counter.counter1;
int num_val = ins->param.counter.counter2;
if (num_key == 1 && num_val == 1) {
assignSimple(0);
}
else {
assignVals(num_key, num_val, 0);
}
}
void VM::assignOperate(Instruction* ins)
{
int num_key = ins->param.counter.counter1;
int num_val = ins->param.counter.counter2;
if (num_key == 1 && num_val == 1) {
assignSimple(1);
}
else {
assignVals(num_key, num_val, 1);
}
}
void VM::assignSimple(int type)
{
Table* tab = getCurrentClosure()->getTopTable();
Value* key = _stack->popValue();
Value* val = _stack->popValue();
if (key->Type() == Value::TYPE_STRING) {
if (type == 1 && getCurrentClosure()->findUpTables(key, nullptr, &tab) == -1) { //赋值时没有找到就放在全局表中
tab = _state->getGlobalTable();
}
tab->Assign(key, val);
}
else if (key->Type() == Value::TYPE_TABLEVAL) {
((TableValue*)key)->SetValue(val);
}
}
void VM::setLoacalVar(Instruction* ins)
{
_stack->Push(ins->param.name);
}
void VM::getLoacalVar(Instruction* ins)
{
Value* key = ins->param.name;
Value* val = nullptr;
if (getCurrentClosure()->findUpTables(key, &val, nullptr) != -1) {
_stack->Push(val);
}
else {
val = new Nil();
_stack->Push(val);
}
}
void VM::pushValue(Instruction* ins)
{
if (ins->type == InstructionParam::InstructionParamType_Name) {
_stack->Push(ins->param.name);
}
else if (ins->type == InstructionParam::InstructionParamType_Value) {
_stack->Push(ins->param.value);
}
}
void VM::operateNum(Instruction* ins)
{
Value* num1 = _stack->popValue();
Value* num2 = _stack->popValue();
if (num1->Type() == Value::TYPE_NIL ||
num2->Type() == Value::TYPE_NIL) {
printf("error, operate on a nil value\n");
}
Value* ret = nullptr;
if (num1->Type() == Value::TYPE_STRING &&
num2->Type() == Value::TYPE_STRING) {
ret = ((String*)num2)->concat((String*)num1);
}
else if (num1->Type() == Value::TYPE_NUMBER &&
num2->Type() == Value::TYPE_NUMBER) {
double num = 0;
if (ins->op_code == Instruction::OpCode_Plus) {
num = ((Number*)num2)->Get() + ((Number*)num1)->Get();
}
else if (ins->op_code == Instruction::OpCode_Minus) {
num = ((Number*)num2)->Get() - ((Number*)num1)->Get();
}
else if (ins->op_code == Instruction::OpCode_Multiply) {
num = ((Number*)num2)->Get() * ((Number*)num1)->Get();
}
else if (ins->op_code == Instruction::OpCode_Divide) {
num = ((Number*)num2)->Get() / ((Number*)num1)->Get();
}
ret = new Number(num);
}
else {
printf("error, attempt to perform arithmetic on a type that cannot be operated\n");
}
_stack->Push(ret);
}
void VM::operateLogic(Instruction* ins)
{
Number* right = (Number*)_stack->popValue();
Number* left = (Number*)_stack->popValue();
BoolValue* retLogic = new BoolValue();
double num1 = left->Get();
double num2 = right->Get();
if (ins->op_code == Instruction::OpCode_Less) {
retLogic->setLogicVal(num1 < num2);
}
else if (ins->op_code == Instruction::OpCode_Greater) {
retLogic->setLogicVal(num1 > num2);
}
else if (ins->op_code == Instruction::OpCode_LessEqual) {
retLogic->setLogicVal(num1 <= num2);
}
else if (ins->op_code == Instruction::OpCode_GreaterEqual) {
retLogic->setLogicVal(num1 >= num2);
}
else if (ins->op_code == Instruction::OpCode_Equal) {
retLogic->setLogicVal(num1 == num2);
}
else if (ins->op_code == Instruction::OpCode_NotEqual) {
retLogic->setLogicVal(num1 != num2);
}
_stack->Push(retLogic);
}
void VM::funcionRet(Instruction* ins)
{
int num = ins->param.counter.counter1;
Closure* cl = getCurrentClosure();
cl->setActRetNum(num);
}
void VM::ifCompare(Instruction* ins)
{
Value* logic = _stack->popValue();
InstructionValue* leftBlock = (InstructionValue*)_stack->popValue();
InstructionValue* rightBlock = nullptr;
if (ins->param.counter.counter1 > 0) {
rightBlock = (InstructionValue*)_stack->popValue();
}
bool runLeft = true; //除了nil和false其他全为true
if (logic->Type() == Value::TYPE_NIL) {
runLeft = false;
}
else {
if (logic->Type() == Value::TYPE_BOOL) {
runLeft = ((BoolValue*)logic)->getLogicVal();
}
}
if (runLeft) {
runBlockCode(leftBlock);
}
else {
if (rightBlock) {
runBlockCode(rightBlock);
}
}
}
void VM::numericFor(Instruction* ins)
{
Value* valStart = _stack->popValue(); //将控制变量的名压入了栈
Table* top = getCurrentClosure()->getTopTable();
Number* numStart = (Number*)top->GetValue(valStart);
int iStart = ((Number*)top->GetValue(valStart))->GetInteger();
int iEnd = ((Number*)_stack->popValue())->GetInteger();
int iStep = 1;
if (ins->param.counter.counter1 > 0) {
iStep = ((Number*)_stack->popValue())->GetInteger();
}
InstructionValue* block = (InstructionValue*)ins->param.value;
block->setFor(true);
auto xInc = [](int i, int end) {return i <= end; };
auto xDec = [](int i, int end) {return i >= end; };
typedef bool(*Cmp)(int, int);
Cmp xCmp = xInc;
if (iStep < 0) {
xCmp = xDec;
}
for (int i = iStart; xCmp(i, iEnd); i += iStep) {
numStart->SetNumber(i); //改变内部的i的值
top->Assign(valStart, numStart);
runBlockCode(block);
if (block->getBreaked()) {
break;
}
}
}
void VM::genericFor(Instruction* ins)
{
Value* ctrlKey = nullptr;
std::vector<Value*> vtKeys;
int num = ins->param.counter.counter1;
for (int i = 0; i < num; i++) {
vtKeys.push_back(_stack->popValue());
}
ctrlKey = vtKeys[vtKeys.size() - 1];
while (_stack->Size() > 3) { //只要三个数据
_stack->popValue();
}
Value* ctrlVal = _stack->popValue();
Value* data = _stack->popValue();
Value* func = _stack->popValue();
do {
_stack->Push(data); //第一个参数
_stack->Push(ctrlVal); //第二个参数
_stack->Push(func); //迭代函数
Instruction insCall;
insCall.param.counter.counter1 = 2;
insCall.param.counter.counter2 = num;
call(&insCall);
for (int i = num - 1; i >=0; i--) {
_stack->Push(vtKeys[i]);
}
assignVals(num, num, 0);
ctrlVal = getCurrentClosure()->getTopTable()->GetValue(ctrlKey);
if (ctrlVal->Type() == Value::TYPE_NIL) { //控制值为nil
break;
}
InstructionValue* block = (InstructionValue*)ins->param.value;
block->setFor(true);
runBlockCode(ins->param.value);
} while (true);
}
void VM::breakFor(Instruction* ins)
{
}
void VM::enterBlock(Instruction* ins)
{
getCurrentClosure()->addBlockTable();
}
void VM::quitBlock(Instruction* ins)
{
getCurrentClosure()->removeBlockTable();
}
void VM::runBlockCode(Value* val)
{
if (val) {
assert(val->Type() == Value::TYPE_INSTRUCTVAL);
getCurrentClosure()->addBlockTable();
int ret = runCode((InstructionValue*)val);
getCurrentClosure()->removeBlockTable();
if (ret == -1) {
((InstructionValue*)val)->setBreaked(true);
InstructionValue* p = (InstructionValue*)val;
while (p) {
p = p->getParent();
if (p) {
p->setBreaked(true);
if (p->getFor()) {
break;
}
}
}
if (!p) { //找到头了都没有找到循环
printf("run error, break in no loop block!!!\n");
}
}
}
}
void VM::generateBlock(Instruction* ins)
{
Value* val = ins->param.value;
_stack->Push(val);
}
void VM::tableDefine(Instruction* ins)
{
Table* tab = new Table();
for (int i = 0; i < ins->param.counter.counter1; i++) {
Value* key = _stack->popValue();
Value* val = _stack->popValue();
tab->Assign(key, val);
}
_stack->Push(tab);
}
void VM::tableArrIndex(Instruction* ins)
{
}
void VM::tableAccess(Instruction* ins)
{
Value* tabName = _stack->popValue();
Value* member = _stack->popValue();
Value* tab = nullptr;
Value* val = nullptr;
std::string stFiled = ((String*)(ins->param.value))->Get();
if (tabName->Type() == Value::TYPE_TABLE) { //a.b.c 那么a.b就是table
val = ((Table*)tabName)->GetValue(member);
}
else if (tabName->Type() == Value::TYPE_NUMBER) {
printf("attempt to index a number val (filed \'%s\') \n", stFiled.c_str());
}
else {
if (getCurrentClosure()->findUpTables(tabName, &tab, nullptr) != -1) {
if (tab->Type() != Value::TYPE_TABLE) {
printf("%s is not a table\n", ((String*)tabName)->Get().c_str());
}
val = ((Table*)tab)->GetValue(member);
if (!val) {
((Table*)tab)->Assign(member, new Nil());
val = ((Table*)tab)->GetTableValue(member); //用于table赋值 t.a = 1
}
}
else {
if (tabName->Type() == Value::TYPE_NUMBER) {
printf("attempt to index a number val (filed \'%s\') \n", stFiled.c_str());
}
else if (tabName->Type() == Value::TYPE_NIL) {
printf("attempt to index a nil val (filed \'%s\') \n", stFiled.c_str());
}
}
}
if (!val) { //a.b 找不到a和b都得为空
val = new Nil();
}
_stack->Push(val);
}
void VM::negNumber(Instruction* ins)
{
Number* val = (Number*)_stack->popValue();
double num = val->Get() * -1;
val->SetNumber(num);
_stack->Push(val);
}
void VM::lenOfVale(Instruction* ins)
{
BaseLib::len(_state, nullptr);
}