annotate CbC-examples/c-next.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 76e1cf5455ef
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
1 #define NEXT_OP(i) (i->op = *(MVMuint16 *)(i->cur_op), i->cur_op += 2, i->op)
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
2
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
3 typedef unsigned short MVMuint16;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
4 typedef unsigned char MVMuint8;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
5 typedef long* MVMRegister;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
6 typedef void* MVMCompUnit;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
7 typedef void* MVMCallsite;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
8
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
9 typedef struct MVMThreadContext {
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
10 MVMuint8 **interp_cur_op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
11 MVMuint8 **interp_bytecode_start;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
12 MVMRegister **interp_reg_base;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
13 MVMCompUnit **interp_cu;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
14 } MVMThreadContext;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
15
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
16
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
17 typedef struct interp {
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
18 MVMuint16 op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
19 /* Points to the place in the bytecode right after the current opcode. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
20 /* See the NEXT_OP macro for making sense of this */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
21 MVMuint8 *cur_op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
22
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
23 /* The current frame's bytecode start. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
24 MVMuint8 *bytecode_start;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
25
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
26 /* Points to the base of the current register set for the frame we
143
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
27 * are presently in. */
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
28 MVMRegister *reg_base;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
29
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
30 /* Points to the current compilation unit. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
31 MVMCompUnit *cu;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
32
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
33 /* The current call site we're constructing. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
34 MVMCallsite *cur_callsite;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
35
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
36 MVMThreadContext *tc;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
37
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
38 __code (*main_ret)(int, void*);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
39 void *env;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
40
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
41 } INTER,*INTERP;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
42
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
43 __code cbc_no_op(INTERP);
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
44 __code cbc_exit(INTERP);
139
ae07388db637 fix c-next.c segfault
anatofuz
parents: 138
diff changeset
45 __code cbc_next(INTERP);
143
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
46 __code cbc_gc(INTERP);
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
47
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
48 __code (* CODES[])(INTERP) = {
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
49 cbc_no_op,
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
50 cbc_no_op,
143
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
51 cbc_gc,
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
52 cbc_exit,
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
53 };
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
54
143
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
55 void gc(int * p, INTERP i){
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
56 i->reg_base = (MVMRegister *)p;
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
57 return;
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
58 }
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
59
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
60 __code cbc_gc(INTERP i){
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
61 int test = 3;
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
62 gc(&test,i);
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
63 goto cbc_next(i);
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
64 }
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
65
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
66 __code cbc_next(INTERP i){
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
67 __code (*c)(INTERP);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
68 c = CODES[NEXT_OP(i)];
143
76e1cf5455ef add cbc_gc test
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 142
diff changeset
69 // c(i);
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
70 goto c(i);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
71 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
72
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
73 __code cbc_no_op(INTERP i){
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
74 goto cbc_next(i);
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
75 }
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
76
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
77 __code cbc_exit(INTERP i){
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
78 goto i->main_ret(0,i->env);
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
79 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
80
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
81
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
82 int interp_run(MVMThreadContext *tc){
144
8f4e72ab4e11 fix segmentation fault caused by nothing next cur_op to end
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
83 INTER inter = {0,0,0,0,0,0,0,0,0};
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
84 INTERP i = &inter;
144
8f4e72ab4e11 fix segmentation fault caused by nothing next cur_op to end
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
85 MVMuint16 cur_op[] = {0,1,1,0,1,2,3};
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
86 i->main_ret = _CbC_return;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
87 i->env = _CbC_environment;
139
ae07388db637 fix c-next.c segfault
anatofuz
parents: 138
diff changeset
88 i->cur_op = (MVMuint8 *)cur_op;
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
89
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
90 tc->interp_cur_op = &i->cur_op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
91 tc->interp_bytecode_start = &i->bytecode_start;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
92 tc->interp_reg_base = &i->reg_base;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
93 tc->interp_cu = &i->cu;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
94 goto cbc_next(i);
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
95 return 0;
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
96 }
135
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
97
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
98 int main(int argc, char **argv){
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
99 MVMThreadContext tct = {0,0,0,0};
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
100 MVMThreadContext* tc = &tct;
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
101 interp_run(tc);
d23615825742 fix _CbC_return for c-next.c
anatofuz
parents: 134
diff changeset
102 }