-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqvmops.c
More file actions
406 lines (357 loc) · 9.83 KB
/
qvmops.c
File metadata and controls
406 lines (357 loc) · 9.83 KB
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
/*
QVMOPS - Quake3 Virtual Machine Opcodes disassembler
Copyright 2004-2026
https://github.com/thecybermind/qvmops/
3-clause BSD license: https://opensource.org/license/bsd-3-clause
Created By:
Kevin Masterson < k.m.masterson@gmail.com >
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qvm.h"
#include "symbols.h"
#include "util.h"
#include "qvmops.h"
static int process(const char* file);
int main(int argc, char* argv[]) {
char qvmfile[1024];
char mapfile[1024];
char outfile[1024];
int n = 0;
int ret = 0;
printf("qvmops v" QVMOPS_VERSION "\n\n");
// require a filename parameter
if (argc < 2) {
fprintf(stderr, "Usage: %s <file> [mapfile]\n", argv[0]);
return 1;
}
strncpyz(qvmfile, argv[1], sizeof(qvmfile));
// if no map filename given, look for qvm filename with .map extension
if (argc == 2) {
strncpyz(mapfile, argv[1], sizeof(mapfile));
// look for ".qvm"
char* p = strrstr(mapfile, ".qvm");
// if found
if (p)
// change to ".map"
memcpy(p, ".map", 4);
// otherwise, append ".map"
else
strncatz(mapfile, ".map", sizeof(mapfile));
}
// otherwise use provided map filename
else
strncpyz(mapfile, argv[2], sizeof(mapfile));
// try to load map file
parse_map(mapfile);
// try to load qvm file
if (!parse_qvm(qvmfile)) {
fprintf(stderr, "Failed to read QVM file %s", argv[1]);
return 1;
}
// open output file for writing
strncpyz(outfile, argv[1], sizeof outfile);
strncatz(outfile, ".txt", sizeof(outfile));
printf("Processing output file %s...\n", outfile);
process(outfile);
// cleanup
printf("%s written\n", outfile);
return ret;
}
// output header
static void process_header(FILE* h) {
puts("Processing header...");
// output header info
fputs("HEADER\n======\n", h);
fprintf(h, "MAGIC: %X (%s)\n", header.magic, header.magic == VM_MAGIC ? "MAGIC" : "MAGIC_VER2");
fprintf(h, "OPCOUNT: 0x%X (%i)\n", header.opcount, header.opcount);
fprintf(h, "CODEOFF: 0x%X (%i)\n", header.codeoffset, header.codeoffset);
fprintf(h, "CODELEN: 0x%X (%i)\n", header.codelength, header.codelength);
fprintf(h, "DATAOFF: 0x%X (%i)\n", header.dataoffset, header.dataoffset);
fprintf(h, "DATALEN: 0x%X (%i)\n", header.datalen, header.datalen);
fprintf(h, "LITLEN : 0x%X (%i)\n", header.litlen, header.litlen);
fprintf(h, "BSSLEN : 0x%X (%i)\n", header.bsslen, header.bsslen);
#ifdef QVMOPS_VER2
if (header.magic == VM_MAGIC_VER2)
fprintf(h, "JTRGLEN: 0x%X (%i)\n", header2.jtrglen, header2.jtrglen);
#endif
}
// output code segment
static int process_code(FILE* h) {
int last_enter_index = -1;
int semicolon = 0;
symbolmap_t* symbol;
instruction_t* instr = NULL;
puts("Processing code segment...");
fputs("\n\nCODE SEGMENT\n============\n", h);
fputs(" INDEX(XINDEX) OFFSET(XOFFSET) INSTR PARAM\n", h);
// output code info
for (int index = 0; index < instructioncount; index++) {
instr = &instructions[index];
semicolon = 0;
fprintf(h, "%06d(%06x) %06d(%07x) %-9s", index, index, instr->offset, instr->offset, opcodename(instr->opcode));
if (opcodeparamsize(instr->opcode))
fprintf(h, " %-10d", instr->param);
else
fputs(" ", h);
switch (instr->opcode) {
case OP_ENTER: {
last_enter_index = index;
symbol = find_code_symbol(index, -1);
if (symbol)
fputs(" ;", h);
else
fprintf(h, " ; START func%d", index);
semicolon = 1;
while (symbol) {
fprintf(h, " START %s", symbol->symbol);
symbol = find_code_symbol(index, symbol->index);
}
break;
}
case OP_LEAVE: {
if (last_enter_index < 0)
break;
symbol = find_code_symbol(last_enter_index, -1);
if (symbol)
fputs(" ;", h);
else
fprintf(h, " ; END func%d", last_enter_index);
semicolon = 1;
while (symbol) {
fprintf(h, " END %s", symbol->symbol);
symbol = find_code_symbol(last_enter_index, symbol->index);
}
break;
}
case OP_CALL: {
instruction_t* prev_instr;
if (index == 0)
break;
prev_instr = &instructions[index - 1];
if (prev_instr->opcode != OP_CONST)
break;
if (prev_instr->param >= instructioncount)
break;
symbol = find_code_symbol(prev_instr->param, -1);
if (symbol)
fputs(" ;", h);
else {
if (prev_instr->param < 0)
fprintf(h, " ; > trap%d", -prev_instr->param - 1);
else
fprintf(h, " ; > func%d", prev_instr->param);
}
semicolon = 1;
while (symbol) {
fprintf(h, " > %s", symbol->symbol);
symbol = find_code_symbol(prev_instr->param, symbol->index);
}
break;
}
case OP_JUMP: {
instruction_t* prev_instr;
instruction_t* next_instr;
if (index == 0)
break;
prev_instr = &instructions[index - 1];
if (prev_instr->opcode != OP_CONST)
break;
if (prev_instr->param >= instructioncount)
break;
symbol = find_code_symbol(prev_instr->param, -1);
if (symbol) {
fputs(" ;", h);
semicolon = 1;
}
else {
for (int i = prev_instr->param; i >= 0; i--) {
if (instructions[i].opcode == OP_ENTER) {
fprintf(h, " ; > func%d+%d", i, prev_instr->param - i);
semicolon = 1;
break;
}
}
}
while (symbol) {
fprintf(h, " > %s+%d", symbol->symbol, prev_instr->param - symbol->offset);
next_instr = &instructions[prev_instr->param + 1];
if (next_instr->opcode == OP_LEAVE)
fputs(" (return)", h);
symbol = find_code_symbol(prev_instr->param, symbol->index);
}
break;
}
case OP_EQ:
case OP_NE:
case OP_LTI:
case OP_LEI:
case OP_GTI:
case OP_GEI:
case OP_LTU:
case OP_LEU:
case OP_GTU:
case OP_GEU:
case OP_EQF:
case OP_NEF:
case OP_LTF:
case OP_LEF:
case OP_GTF:
case OP_GEF: {
instruction_t* next_instr;
if (index == 0)
break;
symbol = find_code_symbol(instr->param, -1);
if (symbol) {
fputs(" ;", h);
semicolon = 1;
}
else {
for (int i = instr->param; i >= 0; i--) {
if (instructions[i].opcode == OP_ENTER) {
fprintf(h, " ; > func%d+%d", i, instr->param - i);
semicolon = 1;
break;
}
}
}
while (symbol) {
fprintf(h, " > %s+%d", symbol->symbol, instr->param - symbol->offset);
next_instr = &instructions[instr->param + 1];
if (next_instr->opcode == OP_LEAVE)
fputs(" (return)", h);
symbol = find_code_symbol(instr->param, symbol->index);
}
break;
}
case OP_CONST: {
instruction_t* next_instr;
// ignore small literals, not likely memory accesses or jumps
if (instr->param < 1025)
break;
if (instr->param > instructioncount && instr->param > datasize[SEGMENT_DATA] + datasize[SEGMENT_LIT] + datasize[SEGMENT_BSS])
break;
if (index == instructioncount - 1)
break;
next_instr = &instructions[index + 1];
if (next_instr->opcode == OP_LOAD1 ||
next_instr->opcode == OP_LOAD2 ||
next_instr->opcode == OP_LOAD4) {
fprintf(h, " ; (%x)", instr->param);
semicolon = 1;
break;
}
if (next_instr->opcode == OP_CALL ||
next_instr->opcode == OP_JUMP)
break;
symbol = find_data_symbol(instr->param, -1);
if (symbol) {
fputs(" ;", h);
semicolon = 1;
}
while (symbol) {
fprintf(h, " %s+%d (?)", symbol->symbol, instr->param - symbol->offset);
symbol = find_data_symbol(instr->param, symbol->index);
}
break;
}
case OP_LOAD1:
case OP_LOAD2:
case OP_LOAD4: {
if (index == 0)
break;
instruction_t* prev_instr = &instructions[index - 1];
if (prev_instr->opcode != OP_CONST)
break;
symbol = find_data_symbol(prev_instr->param, -1);
if (symbol) {
fputs(" ;", h);
semicolon = 1;
}
while (symbol) {
fprintf(h, " %s+%d", symbol->symbol, prev_instr->param - symbol->offset);
symbol = find_data_symbol(prev_instr->param, symbol->index);
}
break;
}
default:
;
}
// add line number if it exists
symbol = find_line(index, -1);
if (symbol && !semicolon)
fputs(" ;", h);
while (symbol) {
fprintf(h, " [%s]", symbol->symbol);
symbol = find_line(index, symbol->index);
}
fputs("\n", h);
fflush(h);
}
}
static void process_data(FILE* h) {
if (symbolcount[SEGMENT_DATA] + symbolcount[SEGMENT_LIT] + symbolcount[SEGMENT_BSS] == 0)
return;
puts("Processing data segment symbols...");
}
static void process_data_hex(FILE* h) {
uint8_t* p;
puts("Processing data segment hex view...");
fputs("\n\nDATA SEGMENT\n============\n", h);
fprintf(h, "LIT segment begins at offset %X (look for | in row %X)\n", datasize[SEGMENT_DATA], datasize[SEGMENT_DATA] & 0xFFFFFFE0);
// start pointer at start of data segment
p = data;
// loop through each byte in data segment
while (p < data + datasize[SEGMENT_DATA] + datasize[SEGMENT_LIT]) {
// print offset
fprintf(h, "%04X ", p - data);
// print hex values
for (int b = 0; b < DATA_ROW_LEN; b++) {
// halfway through the row, print a gap
if (b == DATA_ROW_LEN / 2)
fputs(" ", h);
// if this row runs out of data before the end, print empty spaces
if (p + b >= data + datasize[SEGMENT_DATA] + datasize[SEGMENT_LIT])
fputs(" ", h);
// if this is the split between data and lit, put a bar
else if (p + b == data + datasize[SEGMENT_DATA])
fprintf(h, "|%02X", p[b]);
else
fprintf(h, " %02X", p[b]);
}
fputs(" ", h);
// print characters
for (int b = 0; b < DATA_ROW_LEN; b++) {
// halfway through the row, print a gap
if (b == DATA_ROW_LEN / 2)
fprintf(h, " ");
// if this row runs out of data before the end, print empty spaces
if (p + b >= data + datasize[SEGMENT_DATA] + datasize[SEGMENT_LIT])
fprintf(h, " ");
else
fprintf(h, "%c", printablec(p[b]));
}
fprintf(h, "\n");
fflush(h);
p += DATA_ROW_LEN;
}
}
static int process(const char* file) {
FILE* h;
h = fopen(file, "w");
if (!h || ferror(h)) {
fprintf(stderr, "File not found: %s\n", file);
goto fail;
}
process_header(h);
process_code(h);
process_data(h);
process_data_hex(h);
return 1;
fail:
if (h)
fclose(h);
return 0;
}