forked from luanhailiang/mudos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassembler.c
More file actions
697 lines (629 loc) · 16.3 KB
/
disassembler.c
File metadata and controls
697 lines (629 loc) · 16.3 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
* Dump information about a program, optionally disassembling it.
*/
#include "std.h"
#include "lpc_incl.h"
#include "efuns_incl.h"
#include "simul_efun.h"
#include "comm.h"
#include "swap.h"
#include "lex.h"
#include "file.h"
#include "program.h"
#ifdef F_DUMP_PROG
void dump_prog PROT((program_t *, char *, int));
static void disassemble PROT((FILE *, char *, int, int, program_t *));
static char *disassem_string PROT((char *));
static int CDECL short_compare PROT((CONST void *, CONST void *));
static void dump_line_numbers PROT((FILE *, program_t *));
void
f_dump_prog PROT((void))
{
program_t *prog;
char *where;
int d;
object_t *ob;
int narg = st_num_arg;
if (st_num_arg == 2) {
ob = sp[-1].u.ob;
d = sp->u.number;
where = 0;
} else if (st_num_arg == 3) {
ob = sp[-2].u.ob;
d = sp[-1].u.number;
where = (sp->type == T_STRING) ? sp->u.string : 0;
} else {
ob = sp->u.ob;
d = 0;
where = 0;
}
if (!(prog = ob->prog)) {
error("No program for object.\n");
} else {
if (!where) {
where = "/PROG_DUMP";
}
dump_prog(prog, where, d);
}
pop_n_elems(narg);
}
/* Current flags:
* 1 - do disassembly
* 2 - dump line number table
*/
void
dump_prog P3(program_t *, prog, char *, fn, int, flags)
{
char *fname;
FILE *f;
int i, j;
int num_funcs_total;
fname = check_valid_path(fn, current_object, "dumpallobj", 1);
if (!fname) {
error("Invalid path '%s' for writing.\n", fn);
return;
}
f = fopen(fname, "w");
if (!f) {
error("Unable to open '/%s' for writing.\n", fname);
return;
}
fprintf(f, "NAME: /%s\n", prog->name);
fprintf(f, "INHERITS:\n");
fprintf(f, "\tname fio vio\n");
fprintf(f, "\t---------------- --- ---\n");
for (i = 0; i < (int) prog->num_inherited; i++)
fprintf(f, "\t%-20s %5d %5d\n",
prog->inherit[i].prog->name,
(int)prog->inherit[i].function_index_offset,
(int)prog->inherit[i].variable_index_offset
);
fprintf(f, "PROGRAM:");
for (i = 0; i < (int) prog->program_size; i++) {
if (i % 16 == 0)
fprintf(f, "\n\t%04x: ", (unsigned int) i);
fprintf(f, "%02d ", (unsigned char) prog->program[i]);
}
fputc('\n', f);
fprintf(f, "FUNCTIONS:\n");
fprintf(f, " name offset flags fio # locals # args\n");
fprintf(f, " --------------------- ------ ------- --- -------- ------\n");
num_funcs_total = prog->last_inherited + prog->num_functions_defined;
for (i = 0; i < num_funcs_total; i++) {
char sflags[8];
int flags;
int runtime_index;
function_t *func_entry = find_func_entry(prog, i);
register int low, high, mid;
flags = prog->function_flags[i];
if (flags & FUNC_ALIAS) {
runtime_index = flags & ~FUNC_ALIAS;
sflags[4] = 'a';
}
else {
runtime_index = i;
sflags[4] = '-';
}
flags = prog->function_flags[runtime_index];
sflags[0] = (flags & FUNC_INHERITED) ? 'i' : '-';
sflags[1] = (flags & FUNC_UNDEFINED) ? 'u' : '-';
sflags[2] = (flags & FUNC_STRICT_TYPES) ? 's' : '-';
sflags[3] = (flags & FUNC_PROTOTYPE) ? 'p' : '-';
sflags[5] = (flags & FUNC_TRUE_VARARGS) ? 'V' : '-';
sflags[6] = (flags & FUNC_VARARGS) ? 'v' : '-';
sflags[7] = '\0';
if (flags & FUNC_INHERITED) {
low = 0;
high = prog->num_inherited - 1;
while (high > low) {
mid = (low + high + 1)/2;
if (prog->inherit[mid].function_index_offset > runtime_index)
high = mid -1;
else low = mid;
}
fprintf(f, "%4d: %-20s %5d %7s %5d\n",
i, func_entry->name,
low,
sflags,
runtime_index - prog->inherit[low].function_index_offset);
} else {
fprintf(f, "%4d: %-20s %5d %7s %7d %5d\n",
i, func_entry->name,
runtime_index - prog->last_inherited
,sflags,
(int) func_entry->num_arg,
(int) func_entry->num_local);
}
}
fprintf(f, "VARIABLES:\n");
for (i = 0; i < (int) prog->num_variables_defined; i++)
fprintf(f, "%4d: %-12s\n", i,
prog->variable_table[i]);
fprintf(f, "STRINGS:\n");
for (i = 0; i < (int) prog->num_strings; i++) {
fprintf(f, "%4d: ", i);
for (j = 0; j < 32; j++) {
char c;
if (!(c = prog->strings[i][j]))
break;
else if (c == '\n')
fprintf(f, "\\n");
else
fputc(c, f);
}
fputc('\n', f);
}
if (flags & 1) {
fprintf(f, "\n;;; *** Disassembly ***\n");
disassemble(f, prog->program, 0, prog->program_size, prog);
}
if (flags & 2) {
fprintf(f, "\n;;; *** Line Number Info ***\n");
dump_line_numbers(f, prog);
}
fclose(f);
}
static char *disassem_string P1(char *, str)
{
static char buf[30];
char *b;
int i;
if (!str)
return "0";
b = buf;
for (i = 0; i < 29; i++) {
if (!str[i])
break;
if (str[i] == '\n') {
*b++ = '\\';
*b++ = 'n';
} else {
*b++ = str[i];
}
}
*b++ = 0;
return buf;
}
#define NUM_FUNS (prog->num_functions_defined + prog->last_inherited)
#define NUM_FUNS_D prog->num_functions_defined
#define VARS prog->variable_names
#define NUM_VARS prog->num_variables_total
#define STRS prog->strings
#define NUM_STRS prog->num_strings
#define CLSS prog->classes
static int CDECL
short_compare P2(CONST void *, a, CONST void *, b)
{
int x = *(unsigned short *)a;
int y = *(unsigned short *)b;
return x - y;
}
static char *pushes[] = { "string", "number", "global", "local" };
static void
disassemble P5(FILE *, f, char *, code, int, start, int, end, program_t *, prog)
{
extern int num_simul_efun;
int i, j, instr, is_efun, ri;
long iarg;
unsigned short sarg;
unsigned short offset;
char *pc, buff[256];
int next_func;
short *offsets;
if (start == 0) {
/* sort offsets of functions */
offsets = (short *) malloc(NUM_FUNS_D * 2 * sizeof(short));
for (i = 0; i < (int) NUM_FUNS_D; i++) {
ri = i + prog->last_inherited;
if (prog->function_flags[ri] & FUNC_NO_CODE) {
offsets[i << 1] = end + 1;
}
else {
offsets[i << 1] = prog->function_table[i].address;
}
offsets[(i << 1) + 1] = i;
}
#ifdef _SEQUENT_
qsort((void *) &offsets[0],
#else
qsort((char *) &offsets[0],
#endif
NUM_FUNS_D, sizeof(short) * 2, short_compare);
next_func = 0;
} else {
offsets = 0;
next_func = -1;
}
pc = code + start;
while ((pc - code) < end) {
if ((next_func >= 0) && ((pc - code) >= offsets[next_func])) {
fprintf(f, "\n;; Function %s\n", prog->function_table[offsets[next_func + 1]].name);
next_func += 2;
if (next_func >= ((int) NUM_FUNS_D * 2))
next_func = -1;
}
fprintf(f, "%04x: ", (unsigned) (pc - code));
is_efun = (instr = EXTRACT_UCHAR(pc)) >= BASE;
pc++;
buff[0] = 0;
sarg = 0;
switch (instr) {
case F_PUSH:
fprintf(f, "push ");
i = EXTRACT_UCHAR(pc++);
while (i--) {
j = EXTRACT_UCHAR(pc++);
fprintf(f, "%s %i", pushes[(j & PUSH_WHAT) >> 6],
j & PUSH_MASK);
if (i)
fprintf(f, ", ");
else break;
}
fprintf(f, "\n");
continue;
/* Single numeric arg */
case F_BRANCH_NE:
case F_BRANCH_GE:
case F_BRANCH_LE:
case F_BRANCH_EQ:
case F_BRANCH:
case F_BRANCH_WHEN_ZERO:
case F_BRANCH_WHEN_NON_ZERO:
#ifdef F_LOR
case F_LOR:
case F_LAND:
#endif
COPY_SHORT(&sarg, pc);
offset = (pc - code) + (unsigned short) sarg;
sprintf(buff, "%04x (%04x)", (unsigned) sarg, (unsigned) offset);
pc += 2;
break;
case F_NEXT_FOREACH:
case F_BBRANCH_LT:
COPY_SHORT(&sarg, pc);
offset = (pc - code) - (unsigned short) sarg;
sprintf(buff, "%04x (%04x)", (unsigned) sarg, (unsigned) offset);
pc += 2;
break;
case F_FOREACH:
{
int flags = EXTRACT_UCHAR(pc++);
char *left = "local", *right = "local";
if (flags & FOREACH_LEFT_GLOBAL)
left = "global";
if (flags & FOREACH_RIGHT_GLOBAL)
right = "global";
if (flags & FOREACH_REF) {
if (flags & FOREACH_MAPPING)
right = "ref";
else
left = "ref";
}
if (flags & FOREACH_MAPPING) {
sprintf(buff, "(mapping) %s %i, %s %i",
left, EXTRACT_UCHAR(pc++), right, EXTRACT_UCHAR(pc++));
} else {
sprintf(buff, "(array) %s %i", left, EXTRACT_UCHAR(pc++));
}
break;
}
case F_BBRANCH_WHEN_ZERO:
case F_BBRANCH_WHEN_NON_ZERO:
case F_BBRANCH:
COPY_SHORT(&sarg, pc);
offset = (pc - code) - (unsigned short) sarg;
sprintf(buff, "%04x (%04x)", (unsigned) sarg, (unsigned) offset);
pc += 2;
break;
#ifdef F_JUMP
case F_JUMP:
#endif
#ifdef F_JUMP_WHEN_ZERO
case F_JUMP_WHEN_ZERO:
case F_JUMP_WHEN_NON_ZERO:
#endif
case F_CATCH:
COPY_SHORT(&sarg, pc);
sprintf(buff, "%04x", (unsigned) sarg);
pc += 2;
break;
case F_AGGREGATE:
case F_AGGREGATE_ASSOC:
COPY_SHORT(&sarg, pc);
sprintf(buff, "%d", (int)sarg);
pc += 2;
break;
case F_MAKE_REF:
case F_KILL_REFS:
case F_MEMBER:
case F_MEMBER_LVALUE:
sprintf(buff, "%d", (int)EXTRACT_UCHAR(pc++));
break;
case F_EXPAND_VARARGS:
{
int which = EXTRACT_UCHAR(pc++);
if (which) {
sprintf(buff, "%d from top of stack", which);
} else {
strcpy(buff, "top of stack");
}
}
break;
case F_NEW_EMPTY_CLASS:
case F_NEW_CLASS:
{
int which = EXTRACT_UCHAR(pc++);
strcpy(buff, STRS[CLSS[which].name]);
break;
}
case F_CALL_FUNCTION_BY_ADDRESS:
COPY_SHORT(&sarg, pc);
pc += 3;
if (sarg < NUM_FUNS)
sprintf(buff, "%-12s %5d", function_name(prog, sarg),
(int)sarg);
else
sprintf(buff, "<out of range %d>", (int)sarg);
break;
case F_CALL_INHERITED:
{
program_t *newprog;
newprog = (prog->inherit + EXTRACT_UCHAR(pc++))->prog;
COPY_SHORT(&sarg, pc);
pc += 3;
if (sarg < (newprog->num_functions_defined +
newprog->last_inherited))
sprintf(buff, "%30s::%-12s %5d", newprog->name,
function_name(newprog, sarg), (int) sarg);
else sprintf(buff, "<out of range in %30s - %d>", newprog->name,
(int) sarg);
break;
}
case F_GLOBAL_LVALUE:
case F_GLOBAL:
if ((unsigned) (iarg = EXTRACT_UCHAR(pc)) < NUM_VARS)
sprintf(buff, "%s", variable_name(prog, iarg));
else
sprintf(buff, "<out of range %ld>", iarg);
pc++;
break;
case F_LOOP_INCR:
sprintf(buff, "LV%d", EXTRACT_UCHAR(pc));
pc++;
break;
case F_WHILE_DEC:
COPY_SHORT(&sarg, pc + 1);
offset = (pc - code) - (unsigned short) sarg;
sprintf(buff, "LV%d--, branch %04x (%04x)", EXTRACT_UCHAR(pc),
(unsigned) sarg, (unsigned) offset);
pc += 3;
break;
case F_TRANSFER_LOCAL:
case F_LOCAL:
case F_LOCAL_LVALUE:
case F_VOID_ASSIGN_LOCAL:
case F_REF:
case F_REF_LVALUE:
sprintf(buff, "LV%d", EXTRACT_UCHAR(pc));
pc++;
break;
case F_LOOP_COND_NUMBER:
i = EXTRACT_UCHAR(pc++);
COPY_LONG(&iarg, pc);
pc += SIZEOF_LONG;
COPY_SHORT(&sarg, pc);
offset = (pc - code) - (unsigned short) sarg;
pc += 2;
sprintf(buff, "LV%d < %ld bbranch_when_non_zero %04x (%04x)",
i, iarg, sarg, offset);
break;
case F_LOOP_COND_LOCAL:
i = EXTRACT_UCHAR(pc++);
iarg = *pc++;
COPY_SHORT(&sarg, pc);
offset = (pc - code) - (unsigned short) sarg;
pc += 2;
sprintf(buff, "LV%d < LV%ld bbranch_when_non_zero %04x (%04x)",
i, iarg, sarg, offset);
break;
case F_STRING:
COPY_SHORT(&sarg, pc);
if (sarg < NUM_STRS)
sprintf(buff, "\"%s\"", disassem_string(STRS[sarg]));
else
sprintf(buff, "<out of range %d>", (int)sarg);
pc += 2;
break;
case F_SHORT_STRING:
if (EXTRACT_UCHAR(pc) < NUM_STRS)
sprintf(buff, "\"%s\"", disassem_string(STRS[EXTRACT_UCHAR(pc)]));
else
sprintf(buff, "<out of range %d>", EXTRACT_UCHAR(pc));
pc++;
break;
case F_SIMUL_EFUN:
COPY_SHORT(&sarg, pc);
if (sarg >= num_simul_efun)
sprintf(buff, "<invalid %d> %d\n", sarg, pc[2]);
else
sprintf(buff, "\"%s\" %d", simuls[sarg].func->name, pc[2]);
pc += 3;
break;
case F_FUNCTION_CONSTRUCTOR:
switch (EXTRACT_UCHAR(pc++)) {
case FP_SIMUL:
LOAD_SHORT(sarg, pc);
sprintf(buff, "<simul_efun> \"%s\"", simuls[sarg].func->name);
break;
case FP_EFUN:
LOAD_SHORT(sarg, pc);
sprintf(buff, "<efun> %s", instrs[sarg].name);
break;
case FP_LOCAL:
LOAD_SHORT(sarg, pc);
if (sarg < NUM_FUNS)
sprintf(buff, "<local_fun> %s", function_name(prog, sarg));
else
sprintf(buff, "<local_fun> <out of range %d>", (int)sarg);
break;
case FP_FUNCTIONAL:
case FP_FUNCTIONAL | FP_NOT_BINDABLE:
sprintf(buff, "<functional, %d args>\nCode:", (int)pc[0]);
pc += 3;
break;
case FP_ANONYMOUS:
case FP_ANONYMOUS | FP_NOT_BINDABLE:
COPY_SHORT(&sarg, &pc[2]);
sprintf(buff, "<anonymous function, %d args, %d locals, ends at %04x>\nCode:",
(int)pc[0], (int)pc[1], (int) (pc + 3 + sarg - code));
pc += 4;
break;
}
break;
case F_NUMBER:
COPY_LONG(&iarg, pc);
sprintf(buff, "%ld", iarg);
pc += SIZEOF_LONG;
break;
case F_REAL:
{
float farg;
COPY_FLOAT(&farg, pc);
sprintf(buff, "%f", farg);
pc += 4;
break;
}
case F_SSCANF:
case F_PARSE_COMMAND:
case F_BYTE:
sprintf(buff, "%d", EXTRACT_UCHAR(pc));
pc++;
break;
case F_NBYTE:
sprintf(buff, "-%d", EXTRACT_UCHAR(pc));
pc++;
break;
case F_SWITCH:
{
unsigned char ttype;
unsigned short stable, etable, def;
unsigned int addr;
char *aptr;
char *parg;
ttype = EXTRACT_UCHAR(pc);
COPY_SHORT(&stable, pc + 1);
COPY_SHORT(&etable, pc + 3);
COPY_SHORT(&def, pc + 5);
addr = pc - code;
aptr = pc;
fprintf(f, "switch\n");
fprintf(f, " type: %02x table: %04x-%04x deflt: %04x\n",
(unsigned) ttype, addr + stable,
addr + etable, addr + def);
/* recursively disassemble stuff in switch */
disassemble(f, code, pc - code + 7, addr + stable, prog);
/* now print out table - ugly... */
fprintf(f, " switch table (for %04x)\n",
(unsigned) (pc - code - 1));
if (ttype == 0xfe)
ttype = 0; /* direct lookup */
else if (ttype >> 4 == 0xf)
ttype = 1; /* normal int */
else
ttype = 2; /* string */
pc += stable;
if (ttype == 0) {
i = 0;
while (pc < aptr + etable - 4) {
COPY_SHORT(&sarg, pc);
fprintf(f, "\t%2d: %04x\n", i++, addr + sarg);
pc += 2;
}
COPY_LONG(&iarg, pc);
fprintf(f, "\tminval = %ld\n", iarg);
pc += SIZEOF_LONG;
} else {
while (pc < aptr + etable) {
COPY_PTR(&parg, pc);
COPY_SHORT(&sarg, pc + SIZEOF_PTR);
if (ttype == 1 || !parg) {
if (sarg == 1)
fprintf(f, "\t%-4lu\t<range start>\n", (unsigned long)parg);
else
fprintf(f, "\t%-4lu\t%04x\n", (unsigned long)parg, addr+sarg);
} else {
fprintf(f, "\t\"%s\"\t%04x\n",
disassem_string(parg), addr+sarg);
}
pc += 2 + SIZEOF_PTR;
}
}
continue;
}
case F_EFUNV:
sprintf(buff, "%d", EXTRACT_UCHAR(pc++));
instr = EXTRACT_UCHAR(pc++) + ONEARG_MAX;
break;
case F_EFUN0:
case F_EFUN1:
case F_EFUN2:
case F_EFUN3:
instr = EXTRACT_UCHAR(pc++) + ONEARG_MAX;
if (instrs[instr].min_arg != instrs[instr].max_arg) {
sprintf(buff, "%d", instr - F_EFUN0);
}
break;
case 0:
fprintf(f, "*** zero opcode ***\n");
continue;
}
fprintf(f, "%s %s\n", query_instr_name(instr), buff);
}
if (offsets)
free(offsets);
}
#define INCLUDE_DEPTH 10
static void
dump_line_numbers P2(FILE *, f, program_t *, prog) {
unsigned short *fi;
unsigned char *li_start;
unsigned char *li_end;
unsigned char *li;
int addr;
int sz;
ADDRESS_TYPE s;
if (!prog->line_info) {
load_line_numbers(prog);
if (!prog->line_info) {
fprintf(f, "Failed to load line numbers\n");
return;
}
}
fi = prog->file_info;
li_end = (unsigned char *)(((char *)fi) + fi[0]);
li_start = (unsigned char *)(fi + fi[1]);
fi += 2;
fprintf(f, "\nabsolute line -> (file, line) table:\n");
while (fi < (unsigned short *)li_start) {
fprintf(f, "%i lines from %i [%s]\n", (int)fi[0], (int)fi[1],
prog->strings[fi[1]-1]);
fi += 2;
}
li = li_start;
addr = 0;
fprintf(f,"\naddress -> absolute line table:\n");
while (li < li_end) {
sz = *li++;
#if defined(LPC_TO_C)
COPY_LONG(&s, li);
#elif defined(USE_32BIT_ADDRESSES)
COPY_INT(&s, li);
#else
COPY_SHORT(&s, li);
#endif
li += sizeof(ADDRESS_TYPE);
fprintf(f, "%4x-%4x: %ld\n", addr, addr + sz - 1, (long)s);
addr += sz;
}
}
#endif