-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvm.cpp
570 lines (540 loc) · 17.7 KB
/
cvm.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
//
// Project: cliblisp
// Created by bajdcc
//
#include <iostream>
#include <iomanip>
#include <cstring>
#include "cvm.h"
#include "cast.h"
#include "csub.h"
namespace clib {
cvm::cvm() {
set_free_callback();
builtin();
}
void cvm::builtin() {
global_env = val_obj(ast_env);
global_env->val._env.env = new cval::cenv_t();
global_env->val._env.parent = nullptr;
mem.push_root(global_env);
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s\n", global_env, cast::ast_str(global_env->type).c_str());
#endif
builtin_init();
mem.pop_root();
mem.protect(global_env);
builtin_load();
}
cval *cvm::val_obj(ast_t type) {
auto v = mem.alloc<cval>();
v->type = type;
v->next = nullptr;
return v;
}
cval *cvm::val_str(ast_t type, const char *str) {
auto len = strlen(str);
auto v = (cval *) mem.alloc(sizeof(cval) + len + 1);
v->type = type;
v->next = nullptr;
v->val._string = ((char *) v) + sizeof(cval);
strncpy((char *) v->val._string, str, len);
return v;
}
cval *cvm::val_sub(const char *name, csub sub) {
auto len = strlen(name);
auto v = (cval *) mem.alloc(sizeof(cval) + len + 1);
v->type = ast_sub;
v->next = nullptr;
auto str = ((char *) v) + sizeof(cval);
strncpy(str, name, len);
v->val._sub.vm = this;
v->val._sub.sub = sub;
return v;
}
cval *cvm::val_char(char c) {
auto v = mem.alloc<cval>();
v->type = ast_char;
v->val._char = c;
v->next = nullptr;
return v;
}
cval *cvm::val_sub(cval *val) {
auto name = ((char *) val) + sizeof(cval);
auto sub = val_sub(name, val->val._sub.sub);
sub->val._sub.vm = val->val._sub.vm;
return sub;
}
cval *cvm::val_bool(bool flag) {
auto v = val_obj(ast_int);
v->val._int = flag ? 1 : 0;
return v;
}
static cval **lambda_env(cval *val) {
return (cval **) ((char *) val + sizeof(cval));
}
cval *cvm::val_lambda(cval *param, cval *body, cval *env) {
auto v = (cval *) mem.alloc(sizeof(cval) + sizeof(cval *));
v->type = ast_lambda;
v->next = nullptr;
mem.push_root(v);
v->val._lambda.param = copy(param);
v->val._lambda.body = copy(body);
if (env == global_env) {
*lambda_env(v) = new_env(env);
} else {
auto _env = *lambda_env(v) = new_env(env->val._env.parent);
mem.push_root(_env);
auto &_new_env = *_env->val._env.env;
for (auto &en : *env->val._env.env) {
_new_env.insert(std::make_pair(en.first, copy(en.second)));
}
mem.pop_root();
}
mem.pop_root();
return v;
}
static char *sub_name(cval *val) {
return (char *) val + sizeof(cval);
}
uint cvm::children_size(cval *val) {
if (!val || (val->type != ast_sexpr && val->type != ast_qexpr))
return 0;
return val->val._v.count;
}
cval *cvm::conv(ast_node *node, cval *env) {
if (node == nullptr)
return nullptr;
auto type = (ast_t) node->flag;
switch (type) {
case ast_root: // 根结点,全局声明
return conv(node->child, env);
case ast_sexpr:
if (!node->child)
error("S-exp: missing value");
if (node->child->flag == ast_literal || node->child->flag == ast_sexpr) {
auto v = val_obj(type);
mem.push_root(v);
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s, count: %d\n", v, cast::ast_str(type).c_str(),
cast::children_size(node));
#endif
v->val._v.child = nullptr;
auto i = node->child;
auto local = conv(i, env);
v->val._v.child = local;
v->val._v.count = 1;
i = i->next;
while (i != node->child) {
v->val._v.count++;
local->next = conv(i, env);
local = local->next;
i = i->next;
}
mem.pop_root();
return v;
} else {
error("S-exp: missing literal");
}
break;
case ast_qexpr:
if (!node->child) {
auto v = val_obj(type);
v->val._v.count = 0;
v->val._v.child = nullptr;
return v;
} else {
auto v = val_obj(type);
mem.push_root(v);
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s, count: %d\n", node, cast::ast_str(type).c_str(),
cast::children_size(node));
#endif
v->val._v.child = nullptr;
auto i = node->child;
auto local = conv(i, env);
v->val._v.child = local;
v->val._v.count = 1;
i = i->next;
while (i != node->child) {
v->val._v.count++;
local->next = conv(i, env);
local = local->next;
i = i->next;
}
mem.pop_root();
return v;
}
case ast_string: {
auto v = val_str(type, node->data._string);
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s, val: %s\n", v, cast::ast_str(type).c_str(),
v->val._string);
#endif
return v;
}
case ast_literal: {
auto v = val_str(type, node->data._string);
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s, val: %s\n", v, cast::ast_str(type).c_str(),
v->val._string);
#endif
return v;
}
#if SHOW_ALLOCATE_NODE
#define DEFINE_VAL(t) \
case ast_##t: { \
auto v = val_obj(type); \
v->val._##t = node->data._##t; \
printf("[DEBUG] ALLOC | addr: 0x%p, node: %-10s, val: ", v, cast::ast_str(type).c_str()); \
print(v, std::cout); \
std::cout << std::endl; \
return v; }
#else
#define DEFINE_VAL(t) \
case ast_##t: { \
auto v = val_obj(type); \
v->val._##t = node->data._##t; \
return v; }
#endif
DEFINE_VAL(char)
DEFINE_VAL(uchar)
DEFINE_VAL(short)
DEFINE_VAL(ushort)
DEFINE_VAL(int)
DEFINE_VAL(uint)
DEFINE_VAL(long)
DEFINE_VAL(ulong)
DEFINE_VAL(float)
DEFINE_VAL(double)
#undef DEFINE_VAL
default:
break;
}
error("invalid val type");
return nullptr;
}
status_t cvm::call(csub fun, cval *val, cval *env, cval **ret) {
auto frame = eval_mem.alloc<cframe>();
memset(frame, 0, sizeof(cframe));
frame->fun = fun;
frame->val = val;
frame->env = env;
frame->ret = ret;
eval_stack.push_back(frame);
return s_call;
}
void cvm::prepare(ast_node *node) {
if (!root) {
mem.save_stack();
root = conv(node, global_env);
ret = nullptr;
call(eval, root, global_env, &ret);
}
}
cval *cvm::run(int cycle, int &cycles) {
// 自己实现调用栈
for (auto i = 0; !eval_stack.empty() && i < cycle; i++) {
cycles++;
auto frame = eval_stack.back();
auto r = frame->fun(this, frame);
if (r == s_ret) {
eval_mem.free(frame);
eval_stack.pop_back();
}
if (r == s_sleep) {
return nullptr;
}
}
if (ret == nullptr)
return nullptr;
assert(ret);
root = nullptr;
eval_stack.clear();
eval_mem.clear();
eval_tmp.clear();
return ret;
}
void cvm::error(const string_t &info) {
printf("COMPILER ERROR: %s\n", info.c_str());
throw std::exception();
}
void cvm::print(cval *val, std::ostream &os) {
if (!val)
return;
switch (val->type) {
case ast_root:
break;
case ast_env:
break;
case ast_lambda:
os << "<lambda ";
print(val->val._lambda.param, os);
os << ' ';
print(val->val._lambda.body, os);
os << ">";
break;
case ast_sub:
os << "<subroutine \"" << sub_name(val) << "\">";
break;
case ast_sexpr: {
os << '(';
auto head = val->val._v.child;
while (head) {
print(head, os);
head = head->next;
}
os << ')';
}
break;
case ast_qexpr:
if (val->val._v.count == 0) {
os << "nil";
} else {
os << '`';
auto head = val->val._v.child;
if (val->val._v.count == 1) {
print(head, os);
} else {
os << '(';
while (head) {
print(head, os);
head = head->next;
}
os << ')';
}
}
break;
case ast_literal:
os << val->val._string;
break;
case ast_string:
os << '"' << cast::display_str(val->val._string) << '"';
break;
case ast_char:
if (isprint(val->val._char))
os << '\'' << val->val._char << '\'';
else if (val->val._char == '\n')
os << "'\\n'";
else
os << "'\\x" << std::setiosflags(std::ios::uppercase) << std::hex
<< std::setfill('0') << std::setw(2)
<< (unsigned int) val->val._char << '\'';
break;
case ast_uchar:
os << (unsigned int) val->val._uchar;
break;
case ast_short:
os << val->val._short;
break;
case ast_ushort:
os << val->val._ushort;
break;
case ast_int:
os << val->val._int;
break;
case ast_uint:
os << val->val._uint;
break;
case ast_long:
os << val->val._long;
break;
case ast_ulong:
os << val->val._ulong;
break;
case ast_float:
os << val->val._float;
break;
case ast_double:
os << val->val._double;
break;
}
if (val->next) {
os << ' ';
}
}
void cvm::gc() {
#if SHOW_ALLOCATE_NODE
dump();
#endif
mem.gc();
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] MEM | Alive objects: %lu\n", mem.count());
#endif
}
cval *cvm::copy(cval *val) {
cval *new_val{nullptr};
switch (val->type) {
case ast_root:
case ast_env:
error("not supported");
break;
case ast_lambda:
new_val = val_lambda(val->val._lambda.param, val->val._lambda.body, *lambda_env(val));
break;
case ast_sub:
new_val = val_sub(val);
new_val->val._sub.vm = val->val._sub.vm;
break;
case ast_sexpr:
case ast_qexpr:
new_val = val_obj(val->type);
new_val->val._v.count = val->val._v.count;
if (new_val->val._v.count > 0) {
mem.push_root(new_val);
auto head = val->val._v.child;
new_val->val._v.child = copy(head);
if (val->val._v.count > 1) {
auto _head = new_val->val._v.child;
head = head->next;
while (head) {
_head->next = copy(head);
head = head->next;
_head = _head->next;
}
}
mem.pop_root();
} else {
new_val->val._v.child = nullptr;
}
break;
case ast_literal:
case ast_string:
new_val = val_str(val->type, val->val._string);
break;
case ast_char:
case ast_uchar:
case ast_short:
case ast_ushort:
case ast_int:
case ast_uint:
case ast_long:
case ast_ulong:
case ast_float:
case ast_double:
new_val = val_obj(val->type);
std::memcpy((char *) &new_val->val, (char *) &val->val, sizeof(val->val));
break;
default:
error("invalid copy");
break;
}
#if SHOW_ALLOCATE_NODE
printf("[DEBUG] COPY | addr: 0x%p, node: %-10s, val: ", new_val, cast::ast_str(val->type).c_str());
print(val, std::cout);
std::cout << std::endl;
#endif
return new_val;
}
cval *cvm::calc_symbol(const char *sym, cval *env) {
while (env) {
auto &_env = *env->val._env.env;
auto f = _env.find(sym);
if (f != _env.end()) {
return copy(f->second);
}
env = env->val._env.parent;
}
printf("invalid symbol: %s\n", sym);
error("cannot find symbol");
return nullptr;
}
cval *cvm::def(cval *env, const char *sym, cval *val) {
auto e = env;
while (env) {
auto &_env = *env->val._env.env;
auto f = _env.find(sym);
if (f != _env.end()) {
mem.push_root(env);
auto new_val = copy(val);
mem.pop_root();
mem.unlink(env, f->second);
_env[sym] = new_val;
return new_val;
}
env = env->val._env.parent;
}
mem.push_root(e);
auto new_val = copy(val);
mem.pop_root();
(*e->val._env.env)[sym] = new_val;
return new_val;
}
cval *cvm::new_env(cval *env) {
auto _env = val_obj(ast_env);
_env->val._env.env = new cval::cenv_t();
_env->val._env.parent = env;
return _env;
}
void cvm::set_free_callback() {
#if SHOW_ALLOCATE_NODE
mem.set_callback([](void *ptr) {
cval *val = (cval *) ptr;
printf("[DEBUG] GC | free: 0x%p, node: %-10s, ", ptr, cast::ast_str(val->type).c_str());
if (val->type == ast_sexpr || val->type == ast_qexpr) {
printf("count: %lu\n", children_size(val));
} else if (val->type == ast_literal) {
printf("id: %s\n", val->val._string);
} else if (val->type == ast_env) {
printf("env: %d\n", val->val._env.env->size());
delete val->val._env.env;
} else if (val->type == ast_sub) {
printf("name: %s\n", sub_name(val));
} else {
printf("val: ");
print(val, std::cout);
std::cout << std::endl;
}
});
mem.set_dump_callback([](void *ptr, int level) {
cval *val = (cval *) ptr;
printf("[DEBUG] DUMP | ");
std::cout << std::setfill('_') << std::setw(level << 2) << "";
printf("addr: 0x%p, node: %-10s, ", ptr, cast::ast_str(val->type).c_str());
if (val->type == ast_sexpr || val->type == ast_qexpr) {
printf("count: %lu\n", children_size(val));
} else if (val->type == ast_literal) {
printf("id: %s\n", val->val._string);
} else if (val->type == ast_env) {
printf("env: %d\n", val->val._env.env->size());
} else if (val->type == ast_sub) {
printf("name: %s\n", sub_name(val));
} else {
printf("val: ");
print(val, std::cout);
std::cout << std::endl;
}
});
#else
mem.set_callback([](void *ptr) {
cval *val = (cval *) ptr;
if (val->type == ast_env) {
delete val->val._env.env;
}
});
#endif
}
void cvm::save() {
mem.save_stack();
}
void cvm::restore() {
root = nullptr;
mem.restore_stack();
eval_stack.clear();
eval_mem.clear();
eval_tmp.clear();
}
void cvm::dump() {
#if SHOW_ALLOCATE_NODE
mem.dump(std::cout);
#endif
}
void cvm::reset() {
global_env = nullptr;
mem.clear();
eval_stack.clear();
eval_mem.clear();
eval_tmp.clear();
builtin();
}
}