-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp-switch-mod.c
78 lines (68 loc) · 1.25 KB
/
interp-switch-mod.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
#include "libc.h"
#define dSP register int *sp = stack_pointer
#define TOP (*sp)
int stack;
int *stack_pointer = &stack;
int *var;
#define INSN_NULL 0
#define INSN_DECR 1
#define INSN_CHECK 2
#define INSN_JUMP 3
#define INSN_GET 4
#define INSN_SET 5
typedef struct insn INSN;
struct insn {
unsigned short in_type;
unsigned char in_flags;
unsigned char in_private;
void *in_x1;
};
INSN insns[10];
INSN* initinsns(void)
{
INSN* code = insns;
code[0].in_type = INSN_GET; code[0].in_x1 = var;
code[1].in_type = INSN_DECR;
code[2].in_type = INSN_SET; code[2].in_x1 = var;
code[3].in_type = INSN_CHECK;
code[4].in_type = INSN_JUMP; code[4].in_x1 = &code[0] - 1;
return code;
}
void runinsns(INSN *code)
{
register int *sp = stack_pointer;
register INSN *p = code;
for (;;)
{
switch (p->in_type)
{
case INSN_NULL:
break;
case INSN_DECR:
TOP--;
break;
case INSN_CHECK:
if (!TOP) return;
break;
case INSN_JUMP:
p = (INSN *)(p->in_x1);
break;
case INSN_GET:
TOP = *(int *)(p->in_x1);
break;
case INSN_SET:
*(int *)(p->in_x1) = TOP;
break;
}
p++;
}
}
int main()
{
int var_value;
var = &var_value;
*var = 100000000;
runinsns(initinsns());
printf("%s", "switch\n");
return 0;
}