annotate CbC-examples/c-next.c @ 136:4627f235cf2a

fix c-next example
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:11:56 +0900
parents 71d4882a9ac3
children d22083d7f10b
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
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
16 typedef struct interp {
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
17 MVMuint16 op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
18 /* Points to the place in the bytecode right after the current opcode. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
19 /* See the NEXT_OP macro for making sense of this */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
20 MVMuint8 *cur_op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
21
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
22 /* The current frame's bytecode start. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
23 MVMuint8 *bytecode_start;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
24
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
25 /* Points to the base of the current register set for the frame we
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
26 * * are presently in. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
27 MVMRegister *reg_base;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
28
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
29 /* Points to the current compilation unit. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
30 MVMCompUnit *cu;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
31
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
32 /* The current call site we're constructing. */
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
33 MVMCallsite *cur_callsite;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
34
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
35 MVMThreadContext *tc;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
36
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
37 __code (*ret)(int, void*);
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
136
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
43 __code cbc_next(INTERP i);
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
44
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
45 __code cbc_no_op(INTERP i){
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
46 goto cbc_next(i);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
47 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
48
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
49 __code cbc_exit(INTERP i){
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
50 goto i->main_ret(0,i->env);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
51 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
52
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
53 __code (* CODES[])(INTERP) = {
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
54 cbc_no_op,
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
55 cbc_no_op,
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
56 cbc_exit,
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
57 };
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
58
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
59 __code cbc_next(INTERP i){
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
60 __code (*c)(INTERP);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
61 c = CODES[NEXT_OP(i)];
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
62 goto c(i);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
63 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
64
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
65 int interp_run(MVMThreadContext *tc){
136
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
66 return 0;
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
67 }
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
68
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
69 int main(int argc, char **argv){
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
70 INTER inter = {0,0,0,0,0,0,0,0,0};
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
71 INTERP i = &inter;
136
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
72 MVMuint8 cur_ops[] = {0,1,2,0,3,2};
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
73
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
74 i->main_ret = _CbC_return;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
75 i->env = _CbC_environment;
136
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
76 i->cur_op = cur_ops;
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
77
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
78 MVMThreadContext tc0, *tc;
4627f235cf2a fix c-next example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
79 tc = &tc0;
134
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
80
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
81 tc->interp_cur_op = &i->cur_op;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
82 tc->interp_bytecode_start = &i->bytecode_start;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
83 tc->interp_reg_base = &i->reg_base;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
84 tc->interp_cu = &i->cu;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
85 goto cbc_next(i);
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
86 // return 0;
71d4882a9ac3 add cbc-example
kono
parents:
diff changeset
87 }