-
Notifications
You must be signed in to change notification settings - Fork 0
/
ool.c
5678 lines (4450 loc) · 128 KB
/
ool.c
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <dlfcn.h>
#include <assert.h>
#include "ool.h"
#define HARD_ASSERT assert
#ifndef NDEBUG
#define ASSERT assert
#else
#define ASSERT(x)
#endif
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#if 1
#define PTR_AS_INT(x) ((long long)(x))
#else
#define PTR_AS_INT(x) ((int)(x))
#endif
#define FIELD_OFS(s, f) PTR_AS_INT(&((s *) 0)->f)
#define FIELD_PTR_TO_STRUCT_PTR(p, s, f) ((s *)((char *)(p) - FIELD_OFS(s, f)))
#ifndef NDEBUG
struct {
unsigned vm : 1;
unsigned mem : 1;
unsigned parse : 1;
} debug;
#endif
/***************************************************************************
Design Notes
Passing objects around as parameters to C functions or as return values is OK
**as long as the object is anchored somewhere**, i.e. on the stack, in a VM register,
or as a child of the main module (i.e. accesible from the root set).
Any function that creates a new object must return it in a VM register. The convention
shall be that all return values go in R0, the same as for code methods.
Any function that uses registers internally must save them to the stack and restore them.
This includes saving R0, even though R0 will be used to pass back the result. An incoming
value may be in R0, thus it must be saved at the start of a function, and then dropped
at the end. Unless a function is a leaf function AND it does not modify ANY registers, a
function should save and drop RO, in addition to saving and restoring any registers that
it uses as scratch storage. Thus, the rules for function F are:
(1) if F uses R1 - R7, they must be saved and restored;
(2) if F uses R0, either if F will return a value in R0 or F calls any function that
returns a value in R0, R0 must be saved and dropped or restored, depending whether
F returns a value in R0 or not.
Function nomenclature to make the above easier:
xxx - Utility function
- Purely read-only w.r.t. VM registers
- C return value, or void, and C args
- Example: inst_of()
vm_xxx - VM function
- Operates on registers and stack
- void C type, regular C arguments
- Do not use scratch registers
- Examples: vm_push(reg)
m_xxx - method functions
- void C type, regular C arguments
- Return value in R0, if any
- May use scratch registers, which are saved and restored
- Arguments may reside in registers, so any reagisters used must be saved
- Goes without saying for R1-R7, but if R0 is used as scratch e.g. by calling
another m_xxx function, or if a value will be returned in R0, R0 must be saved on entry
- GENERAL RULE: An m_xxx function must save R0, and either drop it (if returning in R0), or restore it (if not returning in R0)
cm_xxx - Code_Method functions
- Same as m_xxx, except that args are standard, and guaranteed to be on the stack
=> Do not need to save any registers, except those used for scratch, as framework
has placed arguments on the stack
***************************************************************************/
/* Globals */
obj_t *stack, *stack_end; /* Stack start and end */
/* Forward decls */
obj_t dict_at(obj_t dict, obj_t key);
void dict_new_put(obj_t dict, obj_t key, obj_t val);
void dict_at_put(obj_t dict, obj_t key, obj_t val);
void dict_del(obj_t dict, obj_t key);
/***************************************************************************/
/* Fatal error handling */
void
fatal(enum fatal_errcode errcode)
{
char *msg;
switch (errcode) {
case FATAL_ERR_NO_MEM:
msg = "Out of memory";
break;
case FATAL_ERR_STACK_OVERFLOW:
msg = "Stack overflow";
break;
case FATAL_ERR_STACK_UNDERFLOW:
msg = "Stack underflow";
break;
case FATAL_ERR_DOUBLE_ERR:
msg = "Double error";
break;
case FATAL_ERR_BAD_ERR_STREAM:
msg = "Bad strem for error output";
break;
default:
ASSERT(0);
}
fprintf(stderr, "Fatal error: %s\n", msg);
abort();
}
/***************************************************************************/
/* Linked-list handling */
#define LIST_FIRST(list) ((list)->next)
#define LIST_LAST(list) ((list)->prev)
#define LIST_END(list) (list)
unsigned
list_empty(struct list *list)
{
ASSERT((LIST_FIRST(list) == list) == (LIST_LAST(list) == list));
return (LIST_FIRST(list) == list);
}
void
list_init(struct list *list)
{
list->prev = list->next = list;
}
void
list_insert(struct list *node, struct list *before)
{
struct list *p = before->prev;
node->prev = p;
node->next = before;
p->next = before->prev = node;
}
void
list_erase(struct list *node)
{
struct list *p = node->prev, *q = node->next;
p->next = q;
q->prev = p;
}
/***************************************************************************/
obj_t
inst_of(obj_t obj)
{
return (obj ? obj->inst_of : consts.cl.object);
}
unsigned
is_subclass_of(obj_t cl1, obj_t cl2)
{
for ( ; cl1; cl1 = CLASS(cl1)->parent) {
if (cl1 == cl2) return (1);
}
return (0);
}
unsigned
is_kind_of(obj_t obj, obj_t cl)
{
return (is_subclass_of(inst_of(obj), cl));
}
void
cl_inst_init(obj_t cl, obj_t inst, va_list ap)
{
(*CLASS(cl)->inst_init)(cl, inst, ap);
}
void
inst_init_parent(obj_t cl, obj_t inst, va_list ap)
{
cl_inst_init(CLASS(cl)->parent, inst, ap);
}
void meta_metaclass_walk(obj_t inst, void (*func)(obj_t));
void
cl_inst_walk(obj_t cl, obj_t inst, void (*func)(obj_t))
{
if (inst == consts.cl.metaclass) {
meta_metaclass_walk(inst, func);
return;
}
(*CLASS(cl)->inst_walk)(cl, inst, func);
}
void
inst_walk_parent(obj_t cl, obj_t inst, void (*func)(obj_t))
{
cl_inst_walk(CLASS(cl)->parent, inst, func);
}
void
cl_inst_free(obj_t cl, obj_t inst)
{
(*CLASS(cl)->inst_free)(cl, inst);
}
void
inst_free_parent(obj_t cl, obj_t inst)
{
cl_inst_free(CLASS(cl)->parent, inst);
}
void
inst_init(obj_t recvr, ...)
{
va_list ap;
va_start(ap, recvr);
cl_inst_init(inst_of(recvr), recvr, ap);
va_end(ap);
}
struct list obj_list[2];
unsigned obj_list_idx_active, obj_list_idx_marked;
#define OBJ_LIST_ACTIVE (&obj_list[obj_list_idx_active])
#define OBJ_LIST_MARKED (&obj_list[obj_list_idx_marked])
unsigned collectingf;
void
obj_list_swap(void)
{
unsigned temp;
temp = obj_list_idx_active;
obj_list_idx_active = obj_list_idx_marked;
obj_list_idx_marked = temp;
}
void
mem_init(void)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(obj_list); ++i) list_init(&obj_list[i]);
obj_list_idx_active = 0;
obj_list_idx_marked = 1;
}
void collect();
unsigned initf;
#ifndef NDEBUG
struct {
struct {
unsigned alloc_cnt;
unsigned bytes_alloced;
unsigned free_cnt;
unsigned bytes_freed;
unsigned bytes_in_use;
unsigned bytes_in_use_max;
} mem;
struct {
unsigned stack_depth;
unsigned stack_depth_max;
} vm;
} stats;
#define PRINT_VAR(x, f) printf(#x "\t= " f "\n", x)
void
mem_stats_print(void)
{
printf("\nMemory stats:\n");
PRINT_VAR(stats.mem.alloc_cnt, "%d");
PRINT_VAR(stats.mem.bytes_alloced, "%d");
PRINT_VAR(stats.mem.free_cnt, "%d");
PRINT_VAR(stats.mem.bytes_freed, "%d");
PRINT_VAR(stats.mem.bytes_in_use, "%d");
PRINT_VAR(stats.mem.bytes_in_use_max, "%d");
}
void
vm_stats_print(void)
{
printf("\nVM stats:\n");
PRINT_VAR(stats.vm.stack_depth, "%d");
PRINT_VAR(stats.vm.stack_depth_max, "%d");
}
obj_t mem_debug_addr;
void
mem_debug(obj_t obj)
{
unsigned f = 0;
if (obj != mem_debug_addr) return;
f = f;
}
#define MEM_DEBUG(obj) mem_debug(obj)
void
stack_dump(void)
{
obj_t *p, *sp_save = sp;
printf("Stack dump:\n");
for (p = sp; p < stack_end; ++p) {
printf("%p: ", *p);
m_method_call_1(consts.str.print, *p);
printf("\n");
}
ASSERT(sp == sp_save);
}
#else
#define MEM_DEBUG(obj)
#endif
void *
cmalloc(unsigned size)
{
static unsigned alloc_cnt, alloc_limit = (unsigned) -1;
void *result = 0;
if (!initf
&& (alloc_cnt >= alloc_limit || ((result = (void *) malloc(size)) == 0))
) {
collect();
if (alloc_cnt < alloc_limit) alloc_limit = alloc_cnt / 2;
alloc_cnt = 0;
}
if (result == 0 && ((result = malloc(size)) == 0)) {
fatal(FATAL_ERR_NO_MEM);
}
if (!initf) ++alloc_cnt;
#ifndef NDEBUG
++stats.mem.alloc_cnt;
stats.mem.bytes_alloced += size;
stats.mem.bytes_in_use += size;
if (stats.mem.bytes_in_use > stats.mem.bytes_in_use_max) {
stats.mem.bytes_in_use_max = stats.mem.bytes_in_use;
}
#endif
return (result);
}
void
_cfree(void *p, unsigned size)
{
if (p == 0) return;
#ifndef NDEBUG
++stats.mem.free_cnt;
stats.mem.bytes_freed += size;
stats.mem.bytes_in_use -= size;
#endif
free(p);
}
void *
zcmalloc(unsigned size)
{
void *result = cmalloc(size);
memset(result, 0, size);
return (result);
}
void
obj_free(obj_t obj)
{
if (obj == NIL) return;
if (!collectingf) cl_inst_walk(inst_of(obj), obj, obj_release);
list_erase(&obj->list_node);
cl_inst_free(inst_of(obj), obj);
}
void
obj_release(obj_t obj)
{
MEM_DEBUG(obj);
/* Reference loops do exist, so release of an obj with ref_cnt == 0 can
happen, as freeing progresses.
*/
if (obj == NIL || obj->ref_cnt == 0) return;
if (--obj->ref_cnt == 0) obj_free(obj);
}
obj_t
obj_retain(obj_t obj)
{
MEM_DEBUG(obj);
if (obj) {
++obj->ref_cnt;
ASSERT(obj->ref_cnt != 0);
}
return (obj);
}
void
_obj_assign(obj_t *dst, obj_t obj)
{
obj_t old = *dst;
*dst = obj;
obj_release(old);
}
void
obj_assign(obj_t *dst, obj_t obj)
{
_obj_assign(dst, obj_retain(obj));
}
#define OBJ_ASSIGN(dst, src) (obj_assign(&(dst), (src)))
obj_t
obj_alloc(obj_t cl)
{
obj_t result;
result = (obj_t) zcmalloc(CLASS(cl)->inst_size);
list_insert(&result->list_node, LIST_END(OBJ_LIST_ACTIVE));
OBJ_ASSIGN(result->inst_of, cl);
return (result);
}
void
obj_mark(obj_t obj)
{
if (obj == NIL) return;
obj_retain(obj);
if (obj->ref_cnt > 1) return;
list_erase(&obj->list_node);
list_insert(&obj->list_node, LIST_END(OBJ_LIST_MARKED));
cl_inst_walk(inst_of(obj), obj, obj_mark);
}
void
root_walk(void (*func)(obj_t))
{
unsigned i, n;
obj_t *p;
struct root_hdr *r;
for (i = 0; i < ARRAY_SIZE(regs); ++i) (*func)(regs[i]);
(*func)(module_main);
for (p = sp; p < stack_end; ++p) (*func)(*p);
for (r = root; r; r = r->next) {
for (p = (obj_t *)(r + 1), n = r->size; n; --n, ++p) {
(*func)(*p);
}
}
}
void
root_mark(void)
{
/* Zero out all ref cnts */
{
struct list *p;
for (p = LIST_FIRST(OBJ_LIST_ACTIVE); p != LIST_END(OBJ_LIST_ACTIVE); p = p->next) {
obj_t q = FIELD_PTR_TO_STRUCT_PTR(p, struct obj, list_node);
#ifndef NDEBUG
q->old_ref_cnt = q->ref_cnt;
#endif
q->ref_cnt = 0;
}
}
/* Mark everything referenced by root set.
Root set = regs + stack + env + consts
*/
root_walk(obj_mark);
#ifndef NDEBUG
/* Consistency checking */
{
struct list *p;
for (p = LIST_FIRST(OBJ_LIST_ACTIVE); p != LIST_END(OBJ_LIST_ACTIVE); p = p->next) {
obj_t q = FIELD_PTR_TO_STRUCT_PTR(p, struct obj, list_node);
ASSERT(q->ref_cnt == 0);
}
for (p = LIST_FIRST(OBJ_LIST_MARKED); p != LIST_END(OBJ_LIST_MARKED); p = p->next) {
obj_t q = FIELD_PTR_TO_STRUCT_PTR(p, struct obj, list_node);
ASSERT(q->ref_cnt != 0);
ASSERT(q->ref_cnt == q->old_ref_cnt);
}
}
#endif
}
void
collect(void)
{
#ifndef NDEBUG
if (debug.mem) {
printf("collect(): Starting...\n");
mem_stats_print();
}
#endif
collectingf = 1;
root_mark();
/* Free everything left on active list */
{
struct list *p;
while ((p = LIST_FIRST(OBJ_LIST_ACTIVE)) != LIST_END(OBJ_LIST_ACTIVE)) {
obj_t q = FIELD_PTR_TO_STRUCT_PTR(p, struct obj, list_node);
obj_free(q);
}
}
obj_list_swap(); /* Swap marked and active lists */
collectingf = 0;
#ifndef NDEBUG
if (debug.mem) {
printf("collect(): done\n");
mem_stats_print();
}
#endif
}
/***************************************************************************/
/* Design Notes
We need a frame mechanism, for tracking history of:
- method calls, for printing backtraces, and resolving symbol lookups (via modules);
- input files, also for backtraces;
- blocks, for resolving symbol lookups;
Symbol resolution:
- locals i.e. blocks first, then globals i.e. modules
- env_at
. Search through frames, newest to oldest, search dict in each BLOCK frame, i.e.
local variables, by invocation
. Search modules, current to parent, search dict in each module, i.e. module namespaces,
by inclusion (parentage)
- env_new_put
. Search through frames, for first BLOCK or MODULE frame, add to dict
- env_at_put
. Find as in env_at; if found, change value, else do env_new_put
*/
#define FRAME_INIT(f, t) \
(f)->type = (t); \
(f)->prev = frp; \
frp = (f)
#define FRAME_POP frp = frp->prev
#define FRAME_JMP_INIT(f, t, a) \
FRAME_INIT(&(f)->base, (t)); \
(f)->sp = sp; \
(a) = setjmp((f)->jmp_buf)
#define FRAME_RESTART_BEGIN { \
struct frame_jmp __frame[1]; \
int errorf; \
FRAME_JMP_INIT(__frame, FRAME_TYPE_RESTART, errorf);
#define FRAME_RESTART_END \
FRAME_POP; \
}
#define FRAME_WHILE_BEGIN { \
struct frame_jmp __frame[1]; \
int while_arg; \
FRAME_JMP_INIT(__frame, FRAME_TYPE_WHILE, while_arg);
#define FRAME_WHILE_POP \
do { FRAME_POP; } while (0)
#define FRAME_WHILE_END \
FRAME_WHILE_POP; \
}
#define FRAME_INPUT_BEGIN(fi) { \
struct frame_input __frame[1]; \
FRAME_INIT(&__frame->base, FRAME_TYPE_INPUT); \
__frame->file = (fi); \
__frame->line = 1; \
yy_inp_push(__frame);
#define FRAME_INPUT_POP \
do { yy_inp_pop(); FRAME_POP; } while (0)
#define FRAME_INPUT_END \
FRAME_INPUT_POP; \
}
#define FRAME_METHOD_CALL_BEGIN(s, a) { \
struct frame_method_call __frame[1]; \
FRAME_INIT(&__frame->base, FRAME_TYPE_METHOD_CALL); \
__frame->cl = NIL; \
__frame->sel = (s); \
__frame->args = (a);
#define FRAME_METHOD_CALL_POP \
do { FRAME_POP; } while (0)
#define FRAME_METHOD_CALL_END \
FRAME_METHOD_CALL_POP; \
}
#define FRAME_BLOCK_BEGIN(d) { \
struct frame_block __frame[1]; \
int block_arg; \
__frame->dict = (d); \
FRAME_JMP_INIT(&__frame->base, FRAME_TYPE_BLOCK, block_arg);
#define FRAME_BLOCK_POP \
do { FRAME_POP; } while (0)
#define FRAME_BLOCK_END \
FRAME_BLOCK_POP; \
}
#define FRAME_MODULE_BEGIN(m) { \
struct frame_module __frame[1]; \
__frame->module = (m); \
__frame->module_prev = module_cur; \
module_cur = __frame->module; \
FRAME_INIT(&__frame->base, FRAME_TYPE_MODULE);
#define FRAME_MODULE_POP \
do { module_cur = frp->module_prev; FRAME_POP; } while (0)
#define FRAME_MODULE_END \
FRAME_MODULE_POP; \
}
struct frame *frp;
obj_t module_main;
void
frame_goto(enum frame_type type, int longjmp_arg)
{
switch (type) {
case FRAME_TYPE_RESTART:
case FRAME_TYPE_WHILE:
case FRAME_TYPE_BLOCK:
break;
default:
HARD_ASSERT(0);
}
for (;;) {
if (frp->type == type) break;
switch (frp->type) {
case FRAME_TYPE_INPUT:
FRAME_INPUT_POP;
continue;
case FRAME_TYPE_METHOD_CALL:
FRAME_METHOD_CALL_POP;
continue;
case FRAME_TYPE_WHILE:
FRAME_WHILE_POP;
continue;
case FRAME_TYPE_BLOCK:
FRAME_BLOCK_POP;
continue;
case FRAME_TYPE_MODULE:
FRAME_MODULE_POP;
continue;
default:
HARD_ASSERT(0);
}
}
vm_dropn(FRAME_JMP(frp)->sp - sp);
longjmp(FRAME_JMP(frp)->jmp_buf, longjmp_arg);
}
#define BLOCK_RETURN (frame_goto(FRAME_TYPE_BLOCK, 1))
enum { WHILE_ARG_CONT = 1, WHILE_ARG_BREAK };
#define WHILE_CONT (frame_goto(FRAME_TYPE_WHILE, WHILE_ARG_CONT))
#define WHILE_BREAK (frame_goto(FRAME_TYPE_WHILE, WHILE_ARG_BREAK))
obj_t
env_at(obj_t s)
{
struct frame *p;
for (p = frp; p; p = p->prev) {
switch (p->type) {
case FRAME_TYPE_BLOCK:
break;
case FRAME_TYPE_MODULE:
break;
default:
;
}
}
error(ERR_SYM_NOT_BOUND, s);
return (NIL);
}
void
env_at_put(obj_t s, obj_t val)
{
obj_t pr, dn;
env_find(s, &pr, &dn);
if (pr) {
OBJ_ASSIGN(CDR(pr), val);
} else {
dict_at_put(dn, s, val);
}
}
void
env_new_put(obj_t s, obj_t val)
{
obj_t dn;
env_find(s, 0, &dn);
dict_at_put(dn, s, val);
}
void
env_del(obj_t s)
{
obj_t dn;
env_find(s, 0, &dn);
dict_del(dn, s);
}
/***************************************************************************/
void
vm_assign(unsigned dst, obj_t val)
{
ASSERT(dst < ARRAY_SIZE(regs));
OBJ_ASSIGN(regs[dst], val);
}
#ifndef NDEBUG
#define VM_STATS_UPDATE_PUSH(n) \
do { \
if ((stats.vm.stack_depth += (n)) > stats.vm.stack_depth_max) { \
stats.vm.stack_depth_max = stats.vm.stack_depth; \
} \
} while (0)
#define VM_STATS_UPDATE_POP(n) (stats.vm.stack_depth -= (n))
#else
#define VM_STATS_UPDATE_PUSH(n)
#define VM_STATS_UPDATE_POP(n)
#endif
#define VM_STACK_CHK_DN(n) do { if ((sp - (n)) < (stack - 8)) error(ERR_STACK_OVERFLOW); } while (0)
#define VM_STACK_CHK_UP(n) do { if ((sp + (n)) > stack_end) fatal(FATAL_ERR_STACK_UNDERFLOW); } while (0)
void
vm_pushl(obj_t obj)
{
VM_STACK_CHK_DN(1);
VM_STATS_UPDATE_PUSH(1);
*--sp = obj_retain(obj);
}
void
vm_push(unsigned src)
{
HARD_ASSERT(src < ARRAY_SIZE(regs));
vm_pushl(regs[src]);
}
void
vm_pushm(unsigned src, unsigned n)
{
obj_t *p;
HARD_ASSERT((src + n) <= ARRAY_SIZE(regs));
VM_STACK_CHK_DN(n);
VM_STATS_UPDATE_PUSH(n);
for (p = ®s[src]; n; --n, ++p) *--sp = obj_retain(*p);
}
void
vm_pop(unsigned dst)
{
ASSERT(dst < ARRAY_SIZE(regs));
VM_STACK_CHK_UP(1);
VM_STATS_UPDATE_POP(1);
_obj_assign(®s[dst], *sp++);
}
void
vm_popm(unsigned dst, unsigned n)
{
obj_t *p;
ASSERT((dst + n) <= ARRAY_SIZE(regs));
VM_STACK_CHK_UP(n);
VM_STATS_UPDATE_POP(n);
for (p = ®s[dst + n - 1]; n; --n, --p) _obj_assign(p, *sp++);
}
void
vm_drop(void)
{
VM_STACK_CHK_UP(1);
VM_STATS_UPDATE_POP(1);
obj_release(*sp++);
}
void
vm_dropn(unsigned n)
{
VM_STACK_CHK_UP(n);
VM_STATS_UPDATE_POP(n);
for (; n; --n) obj_release(*sp++);
}
/***************************************************************************/
unsigned err_depth;
void
bt_print(obj_t outf)
{
struct frame *p;
FILE *fp = _FILE(outf)->fp;
obj_t q;
vm_push(0);
fprintf(fp, "Backtrace:\n");
for (p = frp; p; p = p->prev) {
switch (p->type) {
case FRAME_TYPE_INPUT:
q = FRAME_INPUT(p)->file;
vm_string_tocstr(_FILE(q)->filename);
fprintf(fp, "From file %s, line %u\n", STRING(R0)->data, FRAME_INPUT(p)->line);
break;
case FRAME_TYPE_METHOD_CALL:
fprintf(fp, "[");
if (q = FRAME_METHOD_CALL(p)->cl) {
m_method_call_2(consts.str.printc, q, outf);
} else {
fprintf(fp, "<none>");
}
fprintf(fp, " ");
m_method_call_2(consts.str.printc, FRAME_METHOD_CALL(p)->sel, outf);
fprintf(fp, "] ");
m_method_call_2(consts.str.printc, FRAME_METHOD_CALL(p)->args, outf);
fprintf(fp, "\n");
break;
default:
;
}
}
vm_drop(1);
}
void
error(enum errcode errcode, ...)
{
obj_t outf;
FILE *fp;
va_list ap;
obj_t obj;
if (err_depth > 0) fatal(FATAL_ERR_DOUBLE_ERR);
++err_depth;
vm_push(0);
m_method_call_1(consts.str._stderr, consts.cl.file);
if (!is_kind_of(R0, consts.cl.file)) {
fatal(FATAL_ERR_BAD_ERR_STREAM);
}
fp = _FILE(outf = R0)->fp;
fprintf(fp, "\n");
va_start(ap, errcode);
switch (errcode) {
case ERR_ASSERT:
fprintf(fp, "Assertion failed");
break;
case ERR_SYM_NOT_BOUND:
fprintf(fp, "Symbol not bound: ");
obj = va_arg(ap, obj_t);
m_method_call_2(consts.str.printc, obj, outf);
break;