-
Notifications
You must be signed in to change notification settings - Fork 1
/
Forth-STC-dis-opcode.c
281 lines (227 loc) · 7.7 KB
/
Forth-STC-dis-opcode.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
#include <stdio.h>
#include "Shared.h"
void gen_funcHeader(char *);
LINE_T *new_line(CELL);
LINE_T *make_tag(LINE_T *);
LINE_T *make_tagF(LINE_T *, char *, ...);
LINE_T *make_code(LINE_T *, char *, ...);
LINE_T *make_codeU(LINE_T *, char *, ...);
LINE_T *make_comment(LINE_T *, char *, ...);
void set_tag(CELL, int);
extern BYTE *the_memory;
extern CELL PC, LAST_PC;
void dis_opcode(int opcode)
{
CELL arg1, arg2, arg3;
LINE_T *line = NULL;
// printf("-%04lX,%X-", LAST_PC, opcode);
switch (opcode)
{
case LITERAL:
arg1 = CELL_AT(PC);
PC += CELL_SZ;
line = make_codeU(NULL, "m_push %ld", arg1);
// make_comment(line, "LITERAL");
return;
case FETCH:
line = make_codeU(NULL, "mov ebx, [ebx]", arg1);
make_comment(line, "FETCH");
return;
case STORE:
line = make_codeU(NULL, "call prim_STORE");
return;
case SWAP:
line = make_codeU(NULL, "call prim_SWAP");
return;
case DROP:
line = make_codeU(NULL, "m_pop eax");
make_comment(line, "DROP");
return;
case DUP:
line = make_codeU(NULL, "m_push ebx");
make_comment(line, "DUP");
return;
case SLITERAL:
arg1 = the_memory[PC];
line = make_codeU(NULL, "call prim_SLITERAL");
line = make_comment(line, "(%d), '%s', (0)", arg1, &the_memory[PC+1]);
PC += arg1+2;
return;
case JMP:
arg1 = CELL_AT(PC);
PC += CELL_SZ;
line = make_codeU(NULL, "jmp L%08lX", arg1);
make_comment(line, "JMP");
set_tag(arg1, 0);
return;
case JMPZ:
arg1 = CELL_AT(PC);
PC += CELL_SZ;
line = make_codeU(NULL, "m_pop eax");
line = make_codeU(NULL, "cmp eax, 0");
line = make_codeU(NULL, "je L%08lX", arg1);
make_comment(line, "JMPZ");
set_tag(arg1, 0);
return;
case JMPNZ:
arg1 = CELL_AT(PC);
PC += CELL_SZ;
line = make_codeU(NULL, "m_pop eax");
line = make_codeU(NULL, "cmp eax, 0");
line = make_codeU(NULL, "jne L%08lX", arg1);
make_comment(line, "JMPNZ");
set_tag(arg1, 0);
return;
case CALL:
arg1 = CELL_AT(PC);
PC += CELL_SZ;
line = make_codeU(NULL, "call L%08lX", arg1);
make_comment(line, "CALL %04lX", arg1);
if (the_memory[arg1] == DICTP)
{
arg2 = CELL_AT(arg1+1);
DICT_T *dict = (DICT_T *)&the_memory[arg2];
make_comment(line, "%s", dict->name);
}
set_tag(arg1, 0);
return;
case RET:
line = make_codeU(NULL, "ret");
return;
case OR:
line = make_codeU(NULL, "call prim_OR");
return;
case CLITERAL:
arg1 = the_memory[PC];
// printf("-CL-%04lX,%X-", PC, arg1);
PC += 1;
line = make_codeU(NULL, "m_push %ld", arg1);
make_comment(line, "CLITERAL (%04lX)", arg1);
return;
case CFETCH:
line = make_codeU(NULL, "call prim_CFETCH");
return;
case CSTORE:
line = make_codeU(NULL, "call prim_CSTORE");
return;
case ADD:
line = make_codeU(NULL, "call prim_ADD");
return;
case SUB:
line = make_codeU(NULL, "call prim_SUB");
return;
case MUL:
line = make_codeU(NULL, "call prim_MUL");
return;
case DIV:
line = make_codeU(NULL, "call prim_DIV");
return;
case LT:
line = make_codeU(NULL, "call prim_LT");
return;
case EQ:
line = make_codeU(NULL, "call prim_EQ");
return;
case GT:
line = make_codeU(NULL, "call prim_GT");
return;
case DICTP:
arg2 = LAST_PC;
arg1 = CELL_AT(PC);
PC += CELL_SZ;
LAST_PC = 0;
// return;
{
char buf[256];
DICT_T *dict = (DICT_T *)&the_memory[arg1];
DICT_T *next = (dict->next > 0) ? (DICT_T *)&the_memory[dict->next] : NULL;
arg3 = (next && (next->next > 0)) ? next->XT : 0;
new_line(0);
sprintf(buf, "dict_ENTRY L%04lX, L%04lX, %d, %d, \"%s\"", dict->XT, arg3, dict->flags, dict->len, dict->name);
line = make_codeU(NULL, "%s", buf);
// set_tag(dict->next, 0);
// set_tag(arg2, 0);
line = new_line(dict->XT);
make_tag(line);
// set_tag(dict->XT, 0);
}
return;
case EMIT:
line = make_codeU(NULL, "call prim_EMIT");
return;
case OVER:
line = make_codeU(NULL, "call prim_OVER");
return;
case COMPARE:
line = make_codeU(NULL, "call prim_COMPARE");
return;
case FOPEN:
line = make_codeU(NULL, "call prim_FOPEN");
return;
case FREAD:
line = make_codeU(NULL, "call prim_FREAD");
return;
case FREADLINE:
line = make_codeU(NULL, "call prim_FREADLINE");
return;
case FWRITE:
line = make_codeU(NULL, "call prim_FWRITE");
return;
case FCLOSE:
line = make_codeU(NULL, "call prim_FCLOSE");
return;
case DTOR:
line = make_codeU(NULL, "m_pop eax");
make_comment(line, "DTOR (>R)");
line = make_codeU(NULL, "m_rpush eax");
return;
case RTOD:
line = make_codeU(NULL, "m_rpop eax");
make_comment(line, "RTOD (R>)");
line = make_codeU(NULL, "m_push eax");
return;
case LOGLEVEL:
line = make_codeU(NULL, "call prim_LOGLEVEL");
return;
case AND:
line = make_codeU(NULL, "call prim_AND");
return;
case PICK:
line = make_codeU(NULL, "call prim_PICK");
return;
case DEPTH:
line = make_codeU(NULL, "call prim_DEPTH");
return;
case GETCH:
line = make_codeU(NULL, "call prim_GETCH");
return;
case COMPAREI:
line = make_codeU(NULL, "call prim_COMPAREI");
return;
case USPUSH:
line = make_codeU(NULL, "call prim_USPUSH");
return;
case USPOP:
line = make_codeU(NULL, "call prim_USPOP");
return;
case INC:
line = make_codeU(NULL, "m_onePlus");
return;
case TIMER:
line = make_codeU(NULL, "call prim_GETTICK");
return;
case DEC:
line = make_codeU(NULL, "m_oneMinus");
return;
case BREAK:
line = make_codeU(NULL, "call prim_BREAK");
return;
case RESET:
line = make_codeU(NULL, "call prim_RESET");
return;
case BYE:
line = make_codeU(NULL, "call prim_BYE");
return;
}
return;
}